Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2016-10-04
19:14 • Closed ticket [4d5ae7d88a]: crash in socket plus 7 other changes artifact: 0ae89e25a2 user: dkf
19:12
[4d5ae7d88a] Stop crashes with asynchronous connects to hosts without addresses. check-in: c7f0617280 user: dkf tags: trunk
19:11
[4d5ae7d88a] Stop crashes with asynchronous connects to hosts without addresses. check-in: 6ef0648c3f user: dkf tags: core-8-6-branch
19:06 • Ticket [4d5ae7d88a] crash in socket status still Open with 7 other changes artifact: e172407464 user: dkf
2016-09-27
20:07 • Ticket [4d5ae7d88a]: 4 changes artifact: 64919dd971 user: gahr
20:06
[4d5ae7d88a] Restore default-to-error logic in TcpConnect closed check-in: e954c8aebe user: gahr tags: tkt-4d5ae7d88a
19:54 • Ticket [4d5ae7d88a] crash in socket status still Open with 5 other changes artifact: e1f5f81427 user: gahr
19:50 • New ticket [4d5ae7d88a]. artifact: 418dfa50ec user: gahr

crash in [socket]

Closed / Fixed4d5ae7d88afc99c9039b66c4e942d45ecdb6f986Created 10 years agoUpdated 10 years ago
Descriptionplain2016-09-27 19:50:40
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.
Referenced by3
  • c7f0617280[4d5ae7d88a] Stop crashes with asynchronous connects to hosts without addresses. check-in2016-10-04 19:12:33
  • 6ef0648c3f[4d5ae7d88a] Stop crashes with asynchronous connects to hosts without addresses. check-in2016-10-04 19:11:07
  • e954c8aebe[4d5ae7d88a] Restore default-to-error logic in TcpConnect check-in2016-09-27 20:06:49
gahrplain2016-09-27 19:54:18
Tentatively assigning to Donald, the author of the commit the bisect lead to...
gahrplain2016-09-27 20:07:30
Patch is available in the [tkt-4d5ae7d88a] branch.
dkfwiki2016-10-04 19:06:18

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 TcpConnect 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.

dkfwiki2016-10-04 19:14:25

Fixed now. Still needs someone to look at the overall logic and untangle that, but that's a lower-priority issue.

Log in (anonymous is fine) to add a comment.