| Ticket UUID: | 630533 | |||
| Title: | TkpCloseDisplay panic | |||
| Type: | Bug | Version: | obsolete: 8.4.1 | |
| Submitter: | dgp | Created on: | 2002-10-29 16:00:18 | |
| Subsystem: | 67. Unix Window Operations | Assigned To: | mdejong | |
| Priority: | 8 | Severity: | ||
| Status: | Closed | Last Modified: | 2002-11-15 00:32:32 | |
| Resolution: | Fixed | Closed By: | mdejong | |
| Closed on: | 2002-11-14 17:32:32 | |||
| Description: |
On Linux/Alpha, with Tcl/Tk 8.4.1:
#FILE panic.tcl
proc Die { win } {
if {![string equal . $win]} { return }
exit
}
bind . <Destroy> {Die %W}
#END FILE panic.tcl
$ wish8.4 panic.tcl
<Use the [x] of the wm to close the . window>
input contexts not freed before XCloseIM
Abort
Now for the really weird part, compare with:
#FILE nopanic.tcl
proc Die { win } {
if {. ne $win} { return }
exit
}
bind . <Destroy> {Die %W}
#END FILE nopanic.tcl
$ wish8.4 panic.tcl
<Same [x] wm close as before works fine>
| |||
| User Comments: |
mdejong added on 2002-11-15 00:32:32:
Logged In: YES user_id=90858 Checking into CVS and closing bug report. jenglish added on 2002-11-14 13:23:42: Logged In: YES user_id=68433 After a quick look: marked_destroy.patch looks good. It's much easier to follow the logic with this one (and the HD_* #defines at the top give a nice concise overview of the cleanup process!). mdejong added on 2002-11-14 04:00:52: File Deleted - 34984: File Added - 35316: marked_destroy.patch Logged In: YES user_id=90858 I have created a new patch that will clean things up properly and fix the problem you pointed out where a window is brought back to life on exit (nice catch BTW). I feel much better about this patch since it uses flags to mark which parts have been cleaned up. I looked at the blocks of code that are not guarded and I think they will work fine when called multiple times. If I don't hear of any problems with this patch, I will check it in this weekend. jenglish added on 2002-11-12 11:27:43: Logged In: YES user_id=68433 Well, *that* didn't work. Tk_DestroyWindow gets called (often reentrantly) from numerous places in the core, and apparently not all of them clean up after themselves safely (e.g., freeing a subresource without checking it for NULL first and/or not setting the pointer to the subresource to NULL after freeing it). tkMenubutton.c is one example of this: from the test suite, menubutton .w -cursor crosshair destroy .w ends up calling Tk_DestroyWindow reentrantly. Currently, the inner call bails out early without doing anything, and the resources are freed in the outer call. But if the inner call runs all the way through, subsequent code in DestroyMenuButton bombs out. So the widget teardown sequence is very fragile. This will probably take a thorough code audit to fix. double_destroy.patch might be a good interim solution. There are still lurking problems, but with the patch the circumstances in which they are triggered are much more limited. jenglish added on 2002-11-11 23:34:20: Logged In: YES
user_id=68433
"You mention that there will be a time when the TK_ALREADY_DEAD
is not set during the execution of Tk_DestroyWindow ..."
For example, if a Destroy binding on .w calls [exit], then
.w will be half-dead but unmarked as ALREADY_DEAD when
destroy bindings for other windows earlier in the main
window list are processed. Here's a test case:
toplevel .w1
toplevel .w2
bind .w1 <Destroy> {
puts "%W destroyed" ;
catch { entry .w2.newchild } result ;# <-- this
should fail
puts "$result"
}
bind .w2 <Destroy> { puts "%W destroyed" ; exit }
destroy .w2
Under wish8.4, the attempt to create .w2.newchild fails with
the error "This isn't a Tk application"; after applying
double_destroy.patch it succeeds.
"As far as letting the user invoke Tk_DestroyWindow multiple
times, it is an interesting idea. You want to whip up a patch?"
Sure -- I'll do this later today when I get in to the office.
Actually double_destroy.patch, minus the code to turn
TK_ALREADY_DEAD off and the initial check for
TK_ALREADY_DEAD in Tk_DestroyWindow(), plus (maybe) an
additional guard for code that has to be executed exactly
once is what I'm thinking of.
"[...] it seems wrong to keep the main window list in thread
local storage. After all, the DeleteWindowsExitProc is only
called once for the entire process."
What are the plans for making Tk thread-safe? My impression
was that keeping the main window list in TLS instead of
global storage was a step towards allowing Tk to be loaded
in multiple threads.
mdejong added on 2002-11-11 15:07:48: Logged In: YES user_id=90858 You mention that there will be a time when the TK_ALREADY_DEAD is not set during the execution of Tk_DestroyWindow, but I don't understand where that would be. The flag is checked and then set right at the top of the function. As far as letting the user invoke Tk_DestroyWindow multiple times, it is an interesting idea. You want to whip up a patch? While thinking about all of this, it struct me that it seems wrong to keep the main window list in thread local storage. After all, the DeleteWindowsExitProc is only called once for the entire process. It was created with Tcl_CreateExitHandler not Tcl_CreateThreadExitHandler. It seems like the main window list should be a global per process list protected by a mutex. jenglish added on 2002-11-11 03:46:14: Logged In: YES user_id=68433 "How would the DeleteWindowsExitProc method know which windows were completely destroyed vs those that were only half destroyed?" DeleteWindowsExitProc wouldn't need to know about half-dead windows, Tk_DestroyWindow would account for them. You're right, phase (1) *does* need to be executed for half-dead windows (at least part of it does), since a half-dead window might still have undestroyed children. Breaking Tk_DestroyWindow down further: 1. Initial Cleanup 2. Recursively destroy children [*] 3. More cleanup 4. Generate Destroy event [*] 5. Even more cleanup 6. Unlink this window from its parent 7. Final cleanup The [*]ed lines can invoke arbitrary user code, so all data structures must be in a consistent state at those points. The only stuff in phase (1) is TkFocusDeadWindow(), and code to unlink the window from the main window list (double_destroy.patch moves this into phase 5.) Phase (3) only includes code to destroy the embedded appllication's window for TK_CONTAINER|TK_BOTH_HALVES windows. I guess this should properly be considered part of phase (2), so (3) is empty. Hm, TkFocusDeadWindow gets a [*] too, since it can generate FocusIn/FocusOut events. With double_destroy.patch, there can be a period of time where there are windows which are being destroyed, but don't have the TK_ALREADY_DEAD flag set; and user code (i.e., Destroy bindings) can be executed during this period. I don't know if this will cause any problems, but there are a lot of places in Tk that check this flag. Perhaps the best approach is to always run through the entire function (as in double_destroy.patch), even for windows that have the TK_ALREADY_DEAD flag set, with a separate guard for parts that must be executed exactly once (e.g., TK_DESTROY_EMITTED), and making sure that everything else can be called multiple times. As long as half-dead windows are reachable via child links from a main window, there's no need to keep track of them in a separate list. mdejong added on 2002-11-09 09:59:32: Logged In: YES user_id=90858 The complication here is that Tk_DestroyWindow will also call Tk_DestroyWindow for each child window and one of the children could call exit in a destroy binding. That is why the patch adds the window to the half dead list at the top of the function instead of just before the Destroy event generation. I thought about just turning stage 3 into a function call and invoking that directly from DeleteWindowsExitProc, but that does not work because the window could have some children that had not yet been destroyed. Moving XDestroyIC higher up in the function should also fail for the same reason. The existing code will never call reenter Tk_DestroyWindow, it just loses track of these half dead windows and leaks the resources (I would not call TK_ALREADY_DEAD a reentrancy lock). My approach was to go through the entire function again. I looked at TkFocusDeadWindow and the child destroy loops and I think they will be ok when run twice over the same window. Of course, this is an area that needs careful review. As far as your flag approach goes, I am not sure how it would avoid the need for the half dead list. How would the DeleteWindowsExitProc method know which windows were completely destroyed vs those that were only half destroyed? jenglish added on 2002-11-09 09:21:27: Logged In: YES
user_id=68433
Is the following an accurate summary of the problem?
Tk_DestroyWindow can be broken down into 3 parts:
1. Initial cleanup
2. Generate Destroy event
3. Final cleanup
Phase (2) can cause Tk_DestroyWindow to be called
reentrantly (by way of [exit] in a Destroy binding, via
DeleteWindowsExitProc). Tk currently has a reentrancy lock
(TK_ALREADY_DEAD) to ensure that 1, 2, and 3 get executed
exactly once. The problem is that phase (3) might still be
pending execution when DeleteWindowsExitProc returns.
destroyic-earlier.patch just moves one specific thing that
was causing trouble from phase (3) to phase (1);
double_destroy.patch ensures that all of phase (3) is
executed for every window by the time DeleteWindowsExitProc
is done. double_destroy.patch is a more general solution.
Hm... we need to make sure it's safe for everything in phase
(1) to be executed twice though. (TkFocusDeadWindow might
be a problem -- if this somehow manages to get called on a
half-dead window, it could mean trouble).
Would adding separate reentrancy guards for phase (1) and
(3) be a good idea?
I.e.,
if (!(winPtr->flags & TK_ALREADY_DEAD)) {
winPtr->flags |= ALREADY_DEAD;
1. Initial cleanup
2. Generate Destroy event
}
if (!(winPtr->flags & TK_COMPLETELY_DEAD)) {
winPtr->flags |= TK_COMPLETELY_DEAD;
3. Final cleanup
}
This would also obviate the need to keep track of the
half-dead window list, since that's only being used to turn
the current single reentrancy guard back off.
mdejong added on 2002-11-09 07:00:48: File Added - 34984: double_destroy.patch Logged In: YES user_id=90858 After much searching, I found that the problem was that Tk was not freeing up window resources when exit was getting called in a Destroy binding. The XIM panic is really just a symptom of a larger problem. I think the attached patch will fix the real cause of the problem by freeing up the resources of windows that were only half destroyed. I also added some new test cases that address this problem. Please take a look at this patch and let me know if you see any obvious problems. mdejong added on 2002-11-06 17:08:02: Logged In: YES user_id=90858 I will take a crack at this one since I also have 620808 assigned to me. andreas_kupries added on 2002-10-31 03:28:17: Logged In: YES user_id=75003 Joe, Don is currently on vacation (wedding and honeymoon). You can check this with Mo DeJong too, he was the last one to go over the tk shutdown logic and he also did the changes touching XCloseIM and such. jenglish added on 2002-10-31 03:21:39: File Added - 34289: destroyic-earlier.patch jenglish added on 2002-10-31 03:21:24: Logged In: YES user_id=68433 I can't replicate this on my machine, but it looks like a duplicate of Tk Bug #620808 'wish crashes after "destroy .toplevel"', which also involves calling [exit] in a <Destroy> binding. I'm not sure I *entirely* understand lines 196-198 of tkUnixEvent.c r1.10 -- avoiding a potential crash in XCloseIM by triggering a surefire crash in panic :-) -- but the attached patch to Tk_DestroyWindow() should avoid the condition (for this case anyway). This simply moves the winPtr->inputContext cleanup code earlier in the teardown sequence, to a point _before_ any <Destroy> events are generated. (This is safe to do at any time, since winPtr->inputContext is checked for NULL in all the places where it's referenced.) If I'm following Tk's shutdown logic correctly (a pretty big if :-), this should fix the problem . Don, could you test the patch and see if it actually works? | |||
Attachments:
- marked_destroy.patch [download] added by mdejong on 2002-11-14 04:00:52. [details]
- destroyic-earlier.patch [download] added by jenglish on 2002-10-31 03:21:31. [details]
