Artifacts Associated With Ticket 4d5ae7d88afc99c9
Ticket change [418dfa50ec] (rid 148910) by gahr on 2016-09-27 19:50:40:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/plain"
- comment initialized to:
I nailed down a crash in test http-3.29 to this commit [bf44d3d17ca27c86]. The test tries to open a connection to the localhost IPv6 address and crashes if the machine does not have an IPv6 interface. The changed logic in [bf44d3d17ca27c86] is that the error variable in TcpConnect() is initialized to the value of errno (happens to be 0) instead of being left uninitialized. This changes the implicit initial state from "there's an error unless proved otherwise" to "there's no error unless proved otherwise". I can reproduce the crash with this script: ================================================================== proc connect {sock} { chan close $sock set ::forever 1 } set sock [socket -async ::1 80] fileevent $sock writable [list connect $sock] vwait forever ================================================================== Backtrace: ================================================================== 0x00000000005c2f00 in Tcl_CreateFileHandler (fd=-1, mask=6, proc=0x5bf960 <WrapNotify>, clientData=0x801ceeb90) at tclUnixNotfy.c:630 630 FD_SET(fd, &tsdPtr->checkMasks.readable); Current language: auto; currently minimal (gdb) bt #0 0x00000000005c2f00 in Tcl_CreateFileHandler (fd=-1, mask=6, proc=0x5bf960 <WrapNotify>, clientData=0x801ceeb90) at tclUnixNotfy.c:630 #1 0x00000000005bf76e in TcpWatchProc (instanceData=0x801ceeb90, mask=4) at tclUnixSock.c:1005 #2 0x0000000000537673 in ChanWatch (chanPtr=0x801cee390, mask=4) at /root/fossil/tcl/generic/tclIO.c:507 #3 0x000000000052bab6 in UpdateInterest (chanPtr=0x801cee390) at /root/fossil/tcl/generic/tclIO.c:8486 #4 0x0000000000533432 in Tcl_CreateChannelHandler (chan=0x801cee390, mask=4, proc=0x533610 <TclChannelEventScriptInvoker>, clientData=0x801cef1d0) at /root/fossil/tcl/generic/tclIO.c:8611 #5 0x0000000000533d62 in CreateScriptRecord (interp=0x801c30010, chanPtr=0x801cee390, mask=4, scriptPtr=0x801ce0c00) at /root/fossil/tcl/generic/tclIO.c:8818 #6 0x0000000000533b48 in Tcl_FileEventObjCmd (clientData=0x0, interp=0x801c30010, objc=4, objv=0x801c632f0) at /root/fossil/tcl/generic/tclIO.c:8978 #7 0x000000000040f8b1 in Dispatch (data=0x801ce0ff8, interp=0x801c30010, result=0) at /root/fossil/tcl/generic/tclBasic.c:4358 #8 0x000000000040b42c in TclNRRunCallbacks (interp=0x801c30010, result=0, rootPtr=0x0) at /root/fossil/tcl/generic/tclBasic.c:4391 #9 0x000000000040b149 in Tcl_EvalObjv (interp=0x801c30010, objc=4, objv=0x801c632f0, flags=2097168) at /root/fossil/tcl/generic/tclBasic.c:4121 #10 0x000000000040cc8d in TclEvalEx (interp=0x801c30010, script=0x801cc7a10 "proc connect {sock} {\n chan close $sock\n set ::forever 1\n}\n\nset sock [socket -async ::1 80]\nfileevent $sock writable [list connect $sock]\nvwait forever\n", numBytes=158, flags=0, line=7, clNextOuter=0x0, outerScript=0x801cc7a10 "proc connect {sock} {\n chan close $sock\n set ::forever 1\n}\n\nset sock [socket -async ::1 80]\nfileevent $sock writable [list connect $sock]\nvwait forever\n") at /root/fossil/tcl/generic/tclBasic.c:5260 #11 0x0000000000548d0a in Tcl_FSEvalFileEx (interp=0x801c30010, pathPtr=0x801c62150, encodingName=0x0) at /root/fossil/tcl/generic/tclIOUtil.c:1826 #12 0x000000000055316c in Tcl_MainEx (argc=-1, argv=0x7fffffffea90, appInitProc=0x404fd0 <Tcl_AppInit>, interp=0x801c30010) at /root/fossil/tcl/generic/tclMain.c:424 #13 0x0000000000404fbf in main (argc=2, argv=0x7fffffffea80) at tclAppInit.c:84 ================================================================== Suggested patch, which solves the problem for me: Index: unix/tclUnixSock.c ================================================================== --- unix/tclUnixSock.c +++ unix/tclUnixSock.c @@ -1096,11 +1096,11 @@ Tcl_Interp *interp, /* For error reporting; can be NULL. */ TcpState *statePtr) { socklen_t optlen; int async_callback = statePtr->flags & TCP_ASYNC_PENDING; - int ret = -1, error = errno; + int ret = -1, error = EHOSTUNREACH; int async = statePtr->flags & TCP_ASYNC_CONNECT; if (async_callback) { goto reenter; } ================================================================== With this patch, the script above behaves as follows, similarly to what it did in Tcl 8.5. ================================================================== couldn't open socket: host is unreachable while executing "socket -async ::1 80" invoked from within "set sock [socket -async ::1 80]" (file "sockcrash.tcl" line 6) ================================================================== The test suite passes, with the patch applied. - foundin initialized to: "trunk"
- is_private initialized to: "0"
- login: "gahr"
- priority initialized to: "5 Medium"
- private_contact initialized to: "438e59f1f1c891b29d2528ed82d1e629cecc6336"
- resolution initialized to: "None"
- severity initialized to: "Severe"
- status initialized to: "Open"
- submitter initialized to: "gahr"
- subsystem initialized to: "25. Channel System"
- title initialized to: "crash in [socket]"
- type initialized to: "Bug"
Ticket change [e1f5f81427] (rid 148911) by gahr on 2016-09-27 19:54:18:
- assignee changed to: "dgp"
- icomment:
Tentatively assigning to Donald, the author of the commit the bisect lead to...
- login: "gahr"
- mimetype: "text/plain"
- username: "gahr"
Ticket change [64919dd971] (rid 148914) by gahr on 2016-09-27 20:07:30:
- icomment: "Patch is available in the [tkt-4d5ae7d88a] branch."
- login: "gahr"
- mimetype: "text/plain"
- username: "gahr"
Ticket change [e172407464] (rid 149044) by dkf on 2016-10-04 19:06:18:
- assignee changed to: "dkf"
- icomment:
That looks like a good change to me; the logic to handle what happens when the list of addresses is empty seems weird, and when mixed with asynchronous connection... well, I'm not surprised that things get to the end in a wrong state. I suspect that we ought to consider rewriting the logic of <tt>TcpConnect</tt> to not have a weird jump into the middle of a loop, but instead use an entirely different path. It would at least make my hackles rise less as the basic block graph would become conventional! But that's not to say that we shouldn't apply this patch; it's clearly an improvement as it doesn't initialise a key variable from an unknown source. The problematic pattern appears to also exist in 8.6. I'll sort it out.
- login: "dkf"
- mimetype: "text/x-fossil-wiki"
- priority changed to: "9 Immediate"
- resolution changed to: "Accepted"
- username: "dkf"
Ticket change [0ae89e25a2] (rid 149053) by dkf on 2016-10-04 19:14:25:
- closedate changed to: "2457666.30168137"
- closer changed to: "dkf"
- icomment:
Fixed now. Still needs someone to look at the overall logic and untangle that, but that's a lower-priority issue.
- login: "dkf"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Closed"
- username: "dkf"