|
2012-07-25
| ||
| 02:13 | • Closed ticket [3545365fff]: Crash in io-29.33b (no implicit flush of nonblocking... artifact: 6607409799 user: ferrieux | |
| 01:28 | • Ticket [3545365fff]: 4 changes artifact: 0732dcc5f3 user: ferrieux | |
| 00:58 | • Ticket [3545365fff]: 4 changes artifact: a7f054794e user: twylite | |
| 00:57 | • Ticket [3545365fff]: 4 changes artifact: df52774e8f user: twylite | |
|
2012-07-24
| ||
| 23:59 | • Ticket [3545365fff]: 4 changes artifact: ef620f54bb user: ferrieux | |
| 23:23 | • Ticket [3545365fff]: 4 changes artifact: 1c8319bab7 user: ferrieux | |
| 22:48 | • Ticket [3545365fff]: 4 changes artifact: a183910f7d user: twylite | |
| 22:45 | • Ticket [3545365fff]: 4 changes artifact: 0a97fdf9bb user: ferrieux | |
| 21:20 | • Ticket [3545365fff]: 4 changes artifact: 23e2517acb user: twylite | |
| 20:57 | • Ticket [3545365fff]: 4 changes artifact: 2fdbc7db23 user: twylite | |
| 20:44 | • Ticket [3545365fff]: 4 changes artifact: 79f5d6bb76 user: ferrieux | |
| 20:41 | • Ticket [3545365fff]: 4 changes artifact: 58a01a50dd user: ferrieux | |
| 20:32 | • Ticket [3545365fff]: 4 changes artifact: ae8dcf1850 user: twylite | |
| 20:20 | • Ticket [3545365fff]: 4 changes artifact: 8b29a1aca6 user: ferrieux | |
| 20:05 | • Ticket [3545365fff]: 4 changes artifact: 9a765ab872 user: ferrieux | |
| 19:57 | • Ticket [3545365fff]: 4 changes artifact: a4a611fb0d user: ferrieux | |
| 19:48 | • Ticket [3545365fff]: 4 changes artifact: dda97a97e9 user: twylite | |
| 18:23 | • Ticket [3545365fff]: 5 changes artifact: 7e2e1552e7 user: ferrieux | |
| 01:42 | • Ticket [3545365fff]: 5 changes artifact: f349e57807 user: ferrieux | |
| 01:24 | • Ticket [3545365fff]: 4 changes artifact: 8df599ee69 user: ferrieux | |
|
2012-07-23
| ||
| 20:35 | • Ticket [3545365fff]: 4 changes artifact: 401865c602 user: ferrieux | |
| 20:02 | • Ticket [3545365fff]: 4 changes artifact: 7876b6f9ef user: twylite | |
| 19:48 | • Ticket [3545365fff]: 4 changes artifact: e3b1cc922c user: nijtmans | |
| 19:37 | • Ticket [3545365fff]: 4 changes artifact: 4ef0eefade user: twylite | |
| 19:23 | • Ticket [3545365fff]: 4 changes artifact: d15beabf8e user: nijtmans | |
| 19:19 | • Ticket [3545365fff]: 4 changes artifact: c6be1367c4 user: nijtmans | |
| 19:12 | • Ticket [3545365fff]: 4 changes artifact: e28f3d6788 user: nijtmans | |
| 19:06 | • Ticket [3545365fff]: 4 changes artifact: 1490dc3499 user: twylite | |
| 19:04 | • Ticket [3545365fff]: 4 changes artifact: 42644b807f user: twylite | |
| 18:56 | • Ticket [3545365fff]: 4 changes artifact: 1008441866 user: nijtmans | |
| 18:49 | • Ticket [3545365fff]: 4 changes artifact: 8c765effe1 user: twylite | |
| 18:46 | • Ticket [3545365fff]: 4 changes artifact: 263b1f7346 user: ferrieux | |
| 18:32 | • Ticket [3545365fff]: 4 changes artifact: e337fdcc24 user: nijtmans | |
| 17:23 | • Ticket [3545365fff]: 4 changes artifact: 3041b0d1c2 user: twylite | |
|
2012-07-18
| ||
| 20:02 | • Ticket [3545365fff]: 4 changes artifact: 03d054e67e user: ferrieux | |
| 11:52 | • New ticket [3545365fff]. artifact: 99146b462f user: twylite | |
Crash in io-29.33b (no implicit flush of nonblocking on exit
Closed / Fixed3545365fffffffffffffffffffffffffffffffffCreated 14 years agoUpdated 14 years ago
Descriptionplain2012-07-18 11:52:19
Hi,
io-29.33b crashes on a trunk build from 2012/07/18 using MSVC10, 32-bit tclsh running on Windows 7 64-bit.
Details:
Build setup (32-bit): 'Setting environment for using Microsoft Visual Studio 2010 x86 tools'
Testing platform: Windows 7 (64-bit), PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 30 Stepping 5, GenuineIntel
C:\User\Tcl_BUILD\cmake-2012\tcl-trunk>fossil pull
Server: http://core.tcl.tk/tcl
C:\User\Tcl_BUILD\cmake-2012\tcl-trunk>fossil update
updated-to: f0f8f508748b68a589768e9e7df3beea28279df9 2012-07-17 13:08:18 UTC
tags: trunk
Build commands:
set BUILDENV=VC10
set VER=86b3
set INSTALLDIR=%CD%\Release_%VER%_%BUILDENV%
set OPTS=threads
set TCLSH=%INSTALLDIR%\bin\tclsh86t.exe
mkdir %INSTALLDIR%
pushd win
nmake /f makefile.vc release tcltest OPTS=%OPTS% CFG_ENCODING=\\\"utf-8\\\" INSTALLDIR=%INSTALLDIR%
nmake /f makefile.vc install OPTS=%OPTS% CFG_ENCODING=\\\"utf-8\\\" INSTALLDIR=%INSTALLDIR%
nmake /f makefile.vc test
FAILURE #1: io-29.33b crashes (probably unrelated)
C:/User/Tcl_BUILD/cmake-2012/tcl-trunk/tests/io.test:2746: error: test failed: i
o-29.33b TIP#398, no implicit flush of nonblocking on exit
==== io-29.33b TIP#398, no implicit flush of nonblocking on exit FAILED
==== Contents of test case:
set f [open $path(script) w]
puts $f {
fconfigure stdout -blocking 0
puts -nonewline stdout [string repeat A 655360]
flush stdout
}
close $f
set f [open $path(script2) w]
puts $f {after 2000}
close $f
set t1 [clock milliseconds]
set ff [open "|[list [interpreter] $path(script2)]" w]
catch {unset ::env(TCL_FLUSH_NONBLOCKING_ON_EXIT)}
exec [interpreter] $path(script) >@ $ff
set t2 [clock milliseconds]
close $ff
expr {($t2-$t1)/2000 ? $t2-$t1 : 0}
---- Test generated error; Return code was: 1
---- Return code should have been one of: 0 2
---- errorInfo: child killed: segmentation violation
while executing
"exec [interpreter] $path(script) >@ $ff"
("uplevel" body line 15)
invoked from within
"uplevel 1 $script"
---- errorCode: CHILDKILLED 9996 SIGSEGV {segmentation violation}
==== io-29.33b FAILED
ferrieuxplain2012-07-18 20:02:29
OK, to proceed and get a debugger stack, we need to work a bit ;) As you have seen, the test involves two subprocesses: $path(script) which does a big nonblocking write, and $path(script2) which just sits for two seconds. The test criterion is then, "is the life of the writer short or long". Here, the crash appears in the writer. So, you'll have to instrument it to be attachable by debugger: (1) arrange so that [interpreter] is the debug-compiled executable (2) add a long [after] at the beginning of the $path(script) (3) identify it in task manager and attach Then please come back with a stack trace ;)
twyliteplain2012-07-23 17:23:40
The crash is in the exec'd process - a NULL pointer dereference in PipClose2Proc during the Tcl shutdown (Tcl_FinalizeIOSubsystem).
tcl86.dll!PipeClose2Proc(void * instanceData, Tcl_Interp * interp, int flags) Line 1809 + 0x13 bytesC
if ((!flags || flags == TCL_CLOSE_READ) && (pipePtr->readFile != NULL)) {
instanceData0x00000000void *
pipePtr0x00000000 {nextPtr=??? channel=??? validMask=??? ...}PipeInfo *
tcl86.dll!Tcl_Close(Tcl_Interp * interp, Tcl_Channel_ * chan) Line 3121 + 0x1a bytesC
tcl86.dll!TclFinalizeIOSubsystem() Line 477 + 0xb bytesC
tcl86.dll!Tcl_FinalizeThread() Line 1305C
tcl86.dll!Tcl_Exit(int status) Line 988C
tcl86.dll!Tcl_ExitObjCmd(void * dummy, Tcl_Interp * interp, int objc, Tcl_Obj * const * objv) Line 825 + 0x9 bytesC
nijtmansplain2012-07-23 18:32:46
My suggestion would be to change the following line tclIO.c:
if (chanPtr->typePtr->closeProc == TCL_CLOSE2PROC) {
to:
if (chanPtr->typePtr->closeProc == TCL_CLOSE2PROC && chanPtr->instanceData) {
(in all Tcl 8.4, 8.5 and 8.6)
Reasoning: If chanPtr->instanceData == NULL, close2Proc for sure will
not be able to handle that. Would that solve the problem?
ferrieuxplain2012-07-23 18:46:12
That would seem to work at a superficial level; however, that this can be happening at all (and not always, not on all OSes...) indicates a deeper flaw in the logic, like a subtle race condition. I'd like to understand it before wiping out the symptom ;) Andreas, are you available to have a look ?
twyliteplain2012-07-23 18:49:10
I have to concur with Alexandre here -- checking that instanceData != NULL will prevent the crash, but there's still the question of when and why instanceData becomes NULL, given that PipeClose2Proc() may not be the only code that assumes instanceData is non-NULL.
nijtmansplain2012-07-23 18:56:35
> indicates a deeper flaw in the logic, like a subtle race condition +1. I was thinking about 8.5.12 nearby, extra safety is always good. That doesn't mean that a possible deeper flaw shouldn't be investigated first, that's why I didn't commit it yet. Is the crash only happening in 8.6? So, I fully agree.
twyliteplain2012-07-23 19:04:44
Just done a pull/update to core-8-5-12-rc, building 32-bit Tcl using MSVC10, running on Win 7 64-bit. No problems in io.test.
twyliteplain2012-07-23 19:06:37
Forgot to mention: if I look higher up the stack from the NULL pointer dereference, the CHANNEL_DEAD flag appears to be set in the channel state struct.
nijtmansplain2012-07-23 19:12:17
Yes, saw that as well. In tclIO.c, I see: /* * Finally, we clean up the fields in the channel data * structure since all of them have been deleted already. We * mark the channel with CHANNEL_DEAD to prevent any further * IO operations on it. */ chanPtr->instanceData = NULL; SetFlag(statePtr, CHANNEL_DEAD); It looks like the only way that instanceData can be NULL is when the CHANNEL_DEAD flag is set. So maybe that's the flag to be test for first.
nijtmansplain2012-07-23 19:19:54
New suggestion. Change
if (chanPtr->typePtr->closeProc == TCL_CLOSE2PROC) {
to:
if (chanPtr->typePtr->closeProc == TCL_CLOSE2PROC &&
chanPtr->flags & CHANNEL_DEAD) {
That would be better, because we cannot assume that
chanPtr->instanceData == NULL means that the channel
is dead. Maybe some channel type doesn't need further
internal data, but still supports the close2proc.
Wondering why chanPtr->instanceData is set to NULL
anyway when the channel is dead: the flag should be
sufficient to make sure that no more IO operations
are done on it, so there is no need to clear instanceData
as well. (although, thanks to this NULL setting we found this flaw.....)
nijtmansplain2012-07-23 19:23:34
Of course, I meant:
if ((chanPtr->typePtr->closeProc == TCL_CLOSE2PROC)
&& !(statePtr->flags & CHANNEL_DEAD)) {
twyliteplain2012-07-23 19:37:01
Tcl_Close() doesn't CheckForDeadChannel(), nor do other internals in tclIO.c that call closeProc or close2Proc. If one place does, then all of them should (or we'll just see this bug pop up again in another form). Would it not be safer to leave instanceData untouched, and let PipeClose2Proc (or other platform-specific close) give a "already closed" error, rather than risk a NULL pointer dereference?
nijtmansplain2012-07-23 19:48:30
>Would it not be safer to leave instanceData untouched, +1 > , and let PipeClose2Proc (or other platform-specific close) give a "already closed" > error, rather than risk a NULL pointer dereference? That would mean that EACH close2Proc should implement this, I would prefer to check for CHANNEL_DEAD in the core, and simply not call close2Proc in that case. How about the FlushChannel call? Should that function be called when the channel is DEAD????
twyliteplain2012-07-23 20:02:34
>> , and let PipeClose2Proc (or other platform-specific close) give a >> "already closed" error, rather than risk a NULL pointer dereference? > That would mean that EACH close2Proc should implement this, > I would prefer to check for CHANNEL_DEAD in the core, and > simply not call close2Proc in that case. I agree that CHANNEL_DEAD should be checked in the core. I wasn't suggesting changes to any close2Proc, merely that a close() on an already-closed fd will result in an OS error, which is preferable to a dereference of a NULL pointer.
ferrieuxplain2012-07-23 20:35:49
Gentlemen, while I fully agree with your observations and reasoning, and as stimulating as reverse engineering can be, I believe we should wait for Andreas' timezone to shed a whole lot of extra light on "the big picture": what's the contract at the channel vector-of-functions boundary, who's looking at the flags, who's caring about NULLs, etc. There clearly are dozens of making all this work together; but incrementally modifying the existing code in a way that (1) keeps the original spirit (2) behaves well with extensions outside our control, requires a broader view I'm afraid ;)
ferrieuxplain2012-07-24 01:24:48
After analysis by Andreas, it seems that the contract is that CHANNEL_DEAD should stop FinalizeIO from calling driver-level functions (it does as ChanClose). Clearly the often-tweaked logic in the following test is suspect:
if (!GotFlag(statePtr, CHANNEL_INCLOSE | CHANNEL_CLOSED | CHANNEL_DEAD)
|| GotFlag(statePtr, BG_FLUSH_SCHEDULED)) {
ResetFlag(statePtr, BG_FLUSH_SCHEDULED);
active = 1;
break;
}
Twylite, can you please verify the state of the flags at the point of crash ? Specifically, do we simultaneously have CHANNEL_DEAD and BG_FLUSH_SCHEDULED ?
ferrieuxplain2012-07-24 01:42:29
OK, seeing better now :) When I added the *outside* "|| GotFlag(statePtr, BG_FLUSH_SCHEDULED)" on 2011-08-17, I mis-took CHANNEL_DEAD for "yet another flavour" of CHANNEL_CLOSE, being confused by their variety. Now I understand that CHANNEL_DEAD implies "nothing under the skin", clearly nothing useful can be done in case of a bgflush. As a consequence, the test now handles CHANNEL_DEAD with the top priority it deserves (never selecting a dead channel, just like before 2008). Fixed in trunk with my apologies.
ferrieuxplain2012-07-24 18:23:41
Reported by jdc (thanks Jos !) on the chat after the fix: on Win7+VC10, 32-bit build: ==== io-29.33b TIP#398, no implicit flush of nonblocking on exit FAILED ---- Result was: 2033 ---- Result should have been (exact matching): 0 ==== io-29.33b FAILED back to the slate... (why is that OS dependent ???)
twyliteplain2012-07-24 19:48:39
What is io-29.33b meant to be doing? From what I see it starts a Tcl process running "after 2000", then starts another process with stdout connected to the first process's stdin. The second process dumps 640k of 'A' and explicitly flushes. Why is that meant to take under 2000ms to complete (test expects [expr {($t2-$t1)/2000 ? $t2-$t1 : 0}] == 0)
ferrieuxplain2012-07-24 19:57:20
Because, under TIP #398, a nonblocking channel should no longer be switched back to blocking on exit. The [flush] is a background flush, which under these circumstances should be cancelled, letting the whole exit quickly (under 2000ms). This works correctly in unix, but as Jos has just helped me discovered, the Windows-specific pipe code has a bug. More on it quickly.
ferrieuxplain2012-07-24 20:05:32
As promised, details on the blocking behavior on Windows. On Jos's gdb attached to the nonblocking-write+exit child, we see that the exit is blocked by the ChanClose in FinalizeIO, done on a still-nonblocking channel.
ChanClose itself calls the platform-specific pipe driver PipeClose2Proc, which blocks at the following code:
/*
* Wait for the writer thread to finish the current buffer, then
* terminate the thread and close the handles. If the channel is
* nonblocking, there should be no pending write operations.
*/
WaitForSingleObject(pipePtr->writable, INFINITE);
Apparently the theorem in the comment is proved wrong :(
Andreas, any idea ?
ferrieuxplain2012-07-24 20:20:55
Digging further in the Windows pipe driver, I understand the following: - due to OS limitations (some long gone, hysterical raisins), the pipe driver uses a pair of threads for each pipe - there is some signalling between the main thread and them, at the beginning of an operation - however, once (e.g.) a write is started, a stubborn WriteFile loop will keep trying until it's done or a SIGPIPE comes up the pipe. - this is notably the case when (as in io-29.33b) a background flush runs on a stalled pipe - in the current design it is impossible to cleanly stop the writer thread to cancel the write as per TIP#398 - at the moment I can see two options: (1) try some hackery with a CloseHandle in the main thread, hoping it will interrupt the WriteFile (does it ?); (2) cheat by leaving the thread and pipe to their destiny, marking as CHANNEL_DEAD although not much has been truly deallocated. Expert advice dearly expected.
twyliteplain2012-07-24 20:32:12
In tclWinPipe.c, PipeWriterThread() only calls SetEvent(infoPtr->writable) once the WriteFile() loop has written out all data. i.e. WaitForSingleObject(pipePtr->writable) will only wake once the flush is complete. If the file handle is closed then WriteFile() should fail and the thread should terminate. I tried moving TclpCloseFile(pipePtr->writeFile) in PipeClose2Proc() to above WaitForSingleObject(pipePtr->writable), but that doesn't help, because of this: TclpCloseFile: /* * Don't close the Win32 handle if the handle is a standard channel * during the thread exit process. Otherwise, one thread may kill the * stdio of another. */ Hmm.
ferrieuxplain2012-07-24 20:41:43
Except it should: this check against std channels is conditional: if (!TclInThreadExit()). But maybe we forgot to set the per-thread InExit when doing the process-wide Exit. To check this, please extend the test: if (!(TclInThreadExit()||TclInExit())).
ferrieuxplain2012-07-24 20:44:01
Sorry, got the logic wrong. That should be: if (TclInExit() || !TclInThreadExit() || ...)
twyliteplain2012-07-24 20:57:14
Heh, was just trying something of the sort ;) Okay, with that logic io-29.33b passes. And with my patch for 3545363 (IPv6 + socket server) in place, the entire Tcl test suite passes (other than unrelated failures whose causes I can account for). Hold on for a bit while I verify that my source tree is clean and try that again though ...
twyliteplain2012-07-24 21:20:52
Okay, sources reverted, full rebuild, and full retest (32-bit Tcl on Windows 7 64-bit, built with MSVC10).
I have made 3 changes to trunk:
(1) Merge bug-3545363 and apply my diff (attached to that bug). Testers on Win7 or WinXP+IPv6 are not going to be able to run the full io.test without doing this.
(2) TclpCloseFile() _before_ shutting down the PipeWriterThread
(3) Close stdio channels if TclInExit() in TclpCloseFile().
Result: indeterminate. io-29.33b passes on its own, io.test passes on its own, io-29.33b occasionally fails when the entire test suite is executed.
I don't have more clarity on the "occasionally" at the moment - trying to reproduce the problem.
Caution: with this change PipeClose2Proc becomes non-blocking on exit, even on a blocking channel. If a blocking write is happening in one interp, a different thread that is exiting as part of the Tcl shutdown rip the channel away and close it.
--- tclWinPipe.c
+++ tclWinPipe.c
@@ -820,11 +820,11 @@
* Don't close the Win32 handle if the handle is a standard channel
* during the thread exit process. Otherwise, one thread may kill the
* stdio of another.
*/
- if (!TclInThreadExit()
+ if (TclInExit() || !TclInThreadExit()
|| ((GetStdHandle(STD_INPUT_HANDLE) != filePtr->handle)
&& (GetStdHandle(STD_OUTPUT_HANDLE) != filePtr->handle)
&& (GetStdHandle(STD_ERROR_HANDLE) != filePtr->handle))) {
if (filePtr->handle != NULL &&
CloseHandle(filePtr->handle) == FALSE) {
@@ -1871,10 +1871,15 @@
pipePtr->validMask &= ~TCL_READABLE;
pipePtr->readFile = NULL;
}
if ((!flags || flags & TCL_CLOSE_WRITE)
&& (pipePtr->writeFile != NULL)) {
+ if (TclpCloseFile(pipePtr->writeFile) != 0) {
+ if (errorCode == 0) {
+ errorCode = errno;
+ }
+ }
if (pipePtr->writeThread) {
/*
* Wait for the writer thread to finish the current buffer, then
* terminate the thread and close the handles. If the channel is
* nonblocking, there should be no pending write operations.
@@ -1931,15 +1936,10 @@
CloseHandle(pipePtr->writable);
CloseHandle(pipePtr->startWriter);
CloseHandle(pipePtr->stopWriter);
pipePtr->writeThread = NULL;
}
- if (TclpCloseFile(pipePtr->writeFile) != 0) {
- if (errorCode == 0) {
- errorCode = errno;
- }
- }
pipePtr->validMask &= ~TCL_WRITABLE;
pipePtr->writeFile = NULL;
}
pipePtr->watchMask &= pipePtr->validMask;
ferrieuxplain2012-07-24 22:45:47
As for the TclpClose: it includes a free of a strucure, so instead of moving the TclpClose call up, I'd suggest a direct low-level CloseHandle, leaving the final TclpClose where it is. A double CloseHandle can complain, but not crash:
Index: win/tclWinPipe.c
===================================================================
--- win/tclWinPipe.c
+++ win/tclWinPipe.c
@@ -1878,10 +1878,13 @@
* Wait for the writer thread to finish the current buffer, then
* terminate the thread and close the handles. If the channel is
* nonblocking, there should be no pending write operations.
*/
+ /* ALEX - ugly hack to check inter-thread io cancellation */
+ if (pipePtr->flags & PIPE_ASYNC) CloseHandle (((WinFile *)pipePtr->writeFile)->handle);
+
WaitForSingleObject(pipePtr->writable, INFINITE);
/*
* The thread may already have closed on it's own. Check its exit
* code.
twyliteplain2012-07-24 22:48:04
Intermittent failures in io-29.33b confirmed. Cause not known. About 1 in 5 times I run io-29.33b on its own it will fail. I've made a io-29.33c that reports the actual time delta (and always fails), and I get results like: 29, 28, 35, 29, 2032, 29, ...
I've also tried a completely different approach, which is to signal the PipeWriterThread to stop, and allow the WriteFile() loop in that thread to be interrupted by the signal. I still get the intermittent failures in io-29.33b. Changing the WriteFile() to write at most 4096 bytes at a time (as in the diff below) doesn't help (although it does seem to make the failures less frequent, maybe).
--- tclWinPipe.c
+++ tclWinPipe.c
@@ -1872,10 +1872,17 @@
pipePtr->readFile = NULL;
}
if ((!flags || flags & TCL_CLOSE_WRITE)
&& (pipePtr->writeFile != NULL)) {
if (pipePtr->writeThread) {
+ /*
+ * Set the stop event so that if the writer thread is blocked
+ * in PipeWriterThread it will exit cleanly.
+ */
+
+ SetEvent(pipePtr->stopWriter);
+
/*
* Wait for the writer thread to finish the current buffer, then
* terminate the thread and close the handles. If the channel is
* nonblocking, there should be no pending write operations.
*/
@@ -1888,17 +1895,10 @@
*/
GetExitCodeThread(pipePtr->writeThread, &exitCode);
if (exitCode == STILL_ACTIVE) {
- /*
- * Set the stop event so that if the reader thread is blocked
- * in PipeReaderThread on WaitForMultipleEvents, it will exit
- * cleanly.
- */
-
- SetEvent(pipePtr->stopWriter);
/*
* Wait at most 20 milliseconds for the reader thread to
* close.
*/
@@ -2953,12 +2953,12 @@
/*
* Loop until all of the bytes are written or an error occurs.
*/
- while (toWrite > 0) {
- if (WriteFile(handle, buf, toWrite, &count, NULL) == FALSE) {
+ while (toWrite > 0 && (WAIT_OBJECT_0 != WaitForMultipleObjects(1, wEvents, FALSE, 0)) ) {
+ if (WriteFile(handle, buf, min(toWrite, 4096), &count, NULL) == FALSE) {
infoPtr->writeError = GetLastError();
done = 1;
break;
} else {
toWrite -= count;
ferrieuxplain2012-07-24 23:23:12
Twylite reports still 1 failure out of 20 runs.
Refining to break out of both while loops in PipeWriterThread, by setting toWrite to zero.
Index: win/tclWinPipe.c
===================================================================
--- win/tclWinPipe.c
+++ win/tclWinPipe.c
@@ -1878,10 +1878,16 @@
* Wait for the writer thread to finish the current buffer, then
* terminate the thread and close the handles. If the channel is
* nonblocking, there should be no pending write operations.
*/
+ /* ALEX - ugly hack to check inter-thread io cancellation */
+ if (pipePtr->flags & PIPE_ASYNC) {
+ pipePtr->toWrite=0;
+ CloseHandle (((WinFile *)pipePtr->writeFile)->handle);
+ }
+
WaitForSingleObject(pipePtr->writable, INFINITE);
/*
* The thread may already have closed on it's own. Check its exit
* code.
@@ -3140,8 +3146,9 @@
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
+ * tab-width: 8
* fill-column: 78
* End:
*/
ferrieuxplain2012-07-24 23:59:59
Twylite identified the missing signalling of the stopWriter event !
Update:
Index: win/tclWinPipe.c
===================================================================
--- win/tclWinPipe.c
+++ win/tclWinPipe.c
@@ -1878,10 +1878,17 @@
* Wait for the writer thread to finish the current buffer, then
* terminate the thread and close the handles. If the channel is
* nonblocking, there should be no pending write operations.
*/
+ /* ALEX - ugly hack to check inter-thread io cancellation */
+ if (pipePtr->flags & PIPE_ASYNC) {
+ pipePtr->toWrite=0;
+ CloseHandle (((WinFile *)pipePtr->writeFile)->handle);
+ SetEvent(pipePtr->stopWriter);
+ }
+
WaitForSingleObject(pipePtr->writable, INFINITE);
/*
* The thread may already have closed on it's own. Check its exit
* code.
@@ -3140,8 +3147,9 @@
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
+ * tab-width: 8
* fill-column: 78
* End:
*/
twyliteplain2012-07-25 00:57:02
No fix yet. I've had to delve deeper into how this file works, and it's confirming my suspicions. The pipe handles are not opened with FILE_FLAG_OVERLAPPED, which means they're blocking. This allows for simple blocking operations in the main thread when (! PIPE_ASYNC), but means that when we're in async mode the write is transferred to the writeThread where WriteFile() is blocking. Enter http://msdn.microsoft.com/en-us/library/windows/desktop/aa365683(v=vs.85).aspx, which confirms that a CloseHandle() in one thread will not in fact interrupt synchronous IO on the handle in another thread. On Windows 2008 or Windows 7 we can use CancelSynchronousIo(pipePtr->writeThread) before the CloseHandle to remedy the situation (io-29.33b run 100 times in a loop, all pass). But there is no equivalent function pre-Win2008. If I put a Tcl_Panic in the (WriteFile() == FALSE) branch of the PipeWriterThread, I never hit it, unless I do a CancelSynchronousIo. A Tcl_Panic after the WaitForSingleObject(pipePtr->writable, INFINITE); in PipeClose2Proc() often reveals that the writeThread is gone. This means that the SetEvent(pipePtr->stopWriter) has woken the thread from a WaitForMultipleObjects, and the thread has then terminated (without signalling infoPtr->writable, because it doesn't do that if woken by stopWriter). Adding a signal to prevent this specific block makes the detection window for the bug narrower, but doesn't solve it. According to my current understanding there is no solution without a redesign to allow WriteFile to be non-blocking in the writeThread. I'll continue looking at the problem tomorrow, and see if I can confirm all these details, and if there is a workable solution (or just attempt to minimize the bug window).
twyliteplain2012-07-25 00:58:58
Quick note: TerminateThread() will cancel blocking IO, so that's a worst-case-scenario solution.
ferrieuxplain2012-07-25 01:28:51
TerminateThread: worst case, as you said, since it has all sorts of problems (memory leaks and deadlocks). The FILE_FLAG_OVERLAPPED route seems to be the way to go; however, if we do that, we might wonder whether it is still necessary to have worker threads... ISTR that some older versions of Windows were unable to include a pipe in a WaitForMultipleObjects; I would appreciate an update on this: if all currently supported Windows support it, the we could go back to a purely event-driven async IO scheme similar to the one in unix.
ferrieuxplain2012-07-25 02:13:10
OK, I realize that we have frankly drifted from the original crash (fixed by the commit to trunk) to a more fundamental design flaw of the Windows pipe driver. So it's time to fork off to a new ticket: 3547994. Closing this one as fixed.
Log in (anonymous is fine) to add a comment.