Artifacts Associated With Ticket 3574493fffffffffffffffffffffffffffffffff
Ticket change [818a8c8896] (rid 117057) by ralfixx on 2012-10-04 15:06:15:
- assignee initialized to: "nobody"
- cmimetype initialized to: "text/plain"
- comment initialized to:
This seems related to the change introduced by ID: 1437595. When running the attached code on Linux, the program opens the server socket, waits one second and then exits as expected. When running the same code on Windows-7 against tcl 8.3, the program behaves as above on Linux. When running the same code on Windows-7 against tcl 8.5 (Activestate, or non-threaded makes no difference) the program blocks in the _exit() call. The reason is to be seen in TclFinalize() where on Windows it waits for the socket thread to exit when unloading the TCL DLL, but the corresponding thread either does no longer exist, or does not get the Message, or does not respond to it: win/tclWin32Dll.c: DLLMain() case DLL_PROCESS_DETACH: ... Tcl_Finalize(); => generic/TclEvent.c Tcl_Finalize() => Tcl_FinalizeThread() => TclFinalizeIOSubsystem(); => TclpFinalizeSockets(); => win/tclWinSock.c TclpFinalizeSockets() => PostMessage(tsdPtr->hwnd, SOCKET_TERMINATE, 0, 0); // this blocks: WaitForSingleObject(tsdPtr->readyEvent, INFINITE); The PostMessage() return code is 0 at that point, indicating an error, the corresponding error is 1400/invalid window handle. If I change the code to if (res) WaitForSingleObject(tsdPtr->readyEvent, INFINITE); the program terminates as expected. It seems an error to call WaitForSingleObject() if the PostMessage() call did not succeed, since the Wait() relies on the other thread to signal the Wait(). If the other thread does not get the call, it cannot signal... Proposed patch against tcl8.5.12: --- tcl8.5.12/win/tclWinSock.c2012/09/18 18:43:471.1 +++ tcl8.5.12/win/tclWinSock.c2012/10/04 15:04:50 @@ -461,14 +461,14 @@ if (tsdPtr != NULL) { if (tsdPtr->socketThread != NULL) { if (tsdPtr->hwnd != NULL) { -PostMessage(tsdPtr->hwnd, SOCKET_TERMINATE, 0, 0); +int res = PostMessage(tsdPtr->hwnd, SOCKET_TERMINATE, 0, 0); /* * Wait for the thread to exit. This ensures that we are * completely cleaned up before we leave this function. */ -WaitForSingleObject(tsdPtr->readyEvent, INFINITE); +if (res) WaitForSingleObject(tsdPtr->readyEvent, INFINITE); tsdPtr->hwnd = NULL; } CloseHandle(tsdPtr->socketThread); - foundin initialized to: "None"
- is_private initialized to: "0"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- status initialized to: "Open"
- submitter initialized to: "ralfixx"
- subsystem initialized to: "None"
- title initialized to: "TclpFinalizeSockets hangs on windows "
- type initialized to: "Bug"
Ticket change [ee2d97aac7] (rid 104037) by ralfixx on 2012-10-04 22:06:21:
- icomment:
File Added - 454320: t.c
- login: "ralfixx"
- mimetype: "text/plain"
- username: "ralfixx"
- icomment:
Add attachment "t.c" [d0381d8f7f] (rid 113136) by ralfixx on 2012-10-04 22:06:21.
Ticket change [a6a984b2fa] (rid 98643) by ferrieux on 2012-10-04 22:36:16:
- assignee changed to: "davygrvy"
- icomment:
Sounds reasonable to me, but my knowledge of Windows (if any) is waning, so please Dave can you validate ?
- login: "ferrieux"
- mimetype: "text/plain"
- username: "ferrieux"
Ticket change [9901fd8a23] (rid 110706) by davygrvy on 2012-10-05 00:37:08:
- icomment:
Looks great!
- login: "davygrvy"
- mimetype: "text/plain"
- username: "davygrvy"
- icomment:
Ticket change [bdd482cdf0] (rid 101975) by dkf on 2012-10-05 02:58:17:
- foundin changed to: "obsolete: 8.5.12"
- icomment:
I'd be tempted to write that as: if (PostMessage(...)) { /* the comment... */ WaitForSingleObject(...); } tsdPtr->hwnd = NULL; YMMV. At least the result of PostMessage() is defined to be boolean... - login: "dkf"
- mimetype: "text/plain"
- priority changed to: "8"
- subsystem changed to: "27. Channel Types"
- username: "dkf"
Ticket change [8cf3b2ea1f] (rid 127830) by ralfixx on 2012-10-05 15:14:09:
- icomment:
Of course the terse form Donal suggested is preferable. I had introduced the intermediate var before looking at the exact signature of PostMessage(). And of course it would be nice to know *why* the handle is already invalid at that point, but...
- login: "ralfixx"
- mimetype: "text/plain"
- username: "ralfixx"
- icomment:
Ticket change [b234f185f6] (rid 98290) by ferrieux on 2012-10-06 04:50:06:
- icomment:
Re "it would be nice to know *why*": agreed. Could you please set breakpoints/printfs at strategic places like SocketExitHandler , TclpFinalizeSocket, or after the "while" loop in SocketThread ? I suspect one of the first two is called twice. Also, what about 8.6 ?
- login: "ferrieux"
- mimetype: "text/plain"
- username: "ferrieux"
- icomment:
Ticket change [95a71b0463] (rid 106193) by davygrvy on 2012-10-06 05:05:28:
- icomment:
unloading the DLL, for which a proper Tcl_Finalize() wasn't done before hand, is my guess. This usage of Tcl here is probably embedding and the app isn't being clean. I've seen this problem before with other areas of Tcl when the finalize logic is manipulated subtly, but has major affect in the abortive teardown.
- login: "davygrvy"
- mimetype: "text/plain"
- username: "davygrvy"
- icomment:
Ticket change [7cbf98ae4d] (rid 97133) by dgp on 2012-11-06 01:19:02:
- assignee changed to: "dkf"
- icomment:
Please report whether action on this is needed before an 8.5.13 release.
- login: "dgp"
- mimetype: "text/plain"
- priority changed to: "9 Immediate"
- username: "dgp"
Ticket change [3731da72d7] (rid 107232) by ralfixx on 2012-11-06 01:40:09:
- icomment:
The patch should be applied. Unfortunately I currently have no resources to investigate the "why" as proposed by Alexandre :-/ @David: the usage as in the example program is indeed 'embedded' with no active TCL interpreter, but the error also manifests when a hard exit (C++ exception or signal) tears down the TCL application without properly releasing the TCL resources first. Since the logic of the patch seems clear it should do no harm in regular usage of the TCL library.
- login: "ralfixx"
- mimetype: "text/plain"
- username: "ralfixx"
- icomment:
Add attachment "tclWinSock.c.diff" [ae269c9b8b] (rid 101593) by ralfixx on 2012-11-06 01:40:46.
Ticket change [3ad43f2b4b] (rid 112628) by ralfixx on 2012-11-06 01:40:46:
- icomment:
File Added - 456039: tclWinSock.c.diff
- login: "ralfixx"
- mimetype: "text/plain"
- username: "ralfixx"
- icomment:
Ticket change [efd6a70643] (rid 109070) by dkf on 2012-11-06 05:01:19:
- assignee changed to: "dgp"
- icomment:
You mistake me for someone who understands what's going on. I read the patch a month ago and noted that it was not "in style" especially; I don't understand the semantics (beyond what I can grep for) and don't really use Tcl on Windows very much at the moment anyway.
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
Ticket change [57fa406e01] (rid 117724) by dkf on 2012-11-06 05:01:46:
- icomment:
(The style issue is not a blocker, BTW.)
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [d2bc1f423a] (rid 113584) by dgp on 2012-11-07 23:17:04:
- icomment:
In the demo program, replacing the call _exit(0) with a call to Tcl_Exit(0) will fix things, yes?
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [70b60ed62f] (rid 100537) by dgp on 2012-11-07 23:25:32:
- icomment:
Thanks to apn for help debugging. apn points to docs saying synchronization calls are not welcome in the routines called by DllMain(). So either Tcl finalization ought not indulge in any synchronization matters, or.... .... Tcl_Finalize() ought not be called from DllMain(). The submitted patch is fine, but doesn't really address the fundamental matter in either of these ways.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [f424dda6da] (rid 129856) by dgp on 2012-11-07 23:46:38:
- icomment:
apn points out to me that removing the Tcl_Finalize() call from DllMain() is the solution on the Tcl trunk (8.6). With Tcl 8.6, we apparently more explicitly put the burden of finalization on the embedding program, instead of trying to make automagic happen as the Tcl Dll gets unloaded. I'll commit the patch on the core-8-5-branch as an additional layer of workaround, but programs like the demo here also ought to be revised to take care of Tcl finalization.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [795c051ad3] (rid 90113) by dgp on 2012-11-08 00:30:53:
- closedate changed to: "2012-11-07T17:30:53"
- closer changed to: "dgp"
- icomment:
allow_comments - 1 patched for 8.4.20 and 8.5.13.
- login: "dgp"
- mimetype: "text/plain"
- resolution changed to: "Fixed"
- status changed to: "Closed"
- username: "dgp"