Skip to content
Tcl Source Code View Ticket
Login

View Ticket

fconfigure <sock> -error called twice in Connect

Closed / Fixed3472316fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 15 years ago
Descriptionplain2012-01-11 09:32:04
It appears that if you call fconfigure twice on a reset socket (for example) a call to fconfigure <sock> -error can only be called once to retrieve the error.

[Terminal 1]
T1 $ tclsh
% socket -server sockproc 12345
sock5
% 

[Terminal 2]
T2 $ tclsh
% socket localhost 12345
sock5
% 

[Terminal 1]
% ^C
T1 $

[Terminal 2]
% fconfigure sock5 -error
connection reset by peer
% fconfigure sock5 -error
% 

It seems this causes this if clause to ultimately return an error string which means it's not possible to determine whether the writeable socket has eof and/or threw an error:

    if {
    [eof $state(sock)] ||
    [string length [fconfigure $state(sock) -error]]
    } then {
    Finish $token "connect failed [fconfigure $state(sock) -error]" 1
    } else {

tcl8.5.11/library/http/http.tcl

As an aside, the documentation for socket appears not to state that the error message can only be retrieved once.

If the fconfigure <sock> -error is run twice in quick succession this appears to make no difference (ie I couldn't determine that it was a race condition with sockets still in the OS's memory).
zwischenzugsplain2012-01-11 16:37:25
NB socket documentation states:

CONFIGURATION OPTIONS
       The fconfigure command can be used to query several readonly configuration options for socket channels:

       -error This option gets the current error status of the given socket.  This is useful when you need to determine if an asynchronous connect operation succeeded.  If there was an error, the  error  message  is  returned.   If
              there was no error, an empty string is returned.
ferrieuxplain2012-01-11 22:04:07
It is a documentation bug indeed: the (read-side) -error option is in 1-to-1 mapping with the getsockopt(SO_ERROR) call, which on all platforms I know of is documented as "getting *and* clearing the error status".
So it's not a race condition, it is as documented on the OS side, only Tcl docs failsto propagate the info.

Will fix the doc soon ; fix of the http package is left in its maintainer's hands.
ferrieuxplain2012-01-13 04:02:46
Doc fixed in both 8.[56] branches.
patthoytsplain2012-01-13 04:34:42
I'm not really sure why this didn't just get rolled into the documentation fix. Someone with more familiarity with fossil can apply this:
Index: library/http/http.tcl
===================================================================
--- library/http/http.tcl
+++ library/http/http.tcl
@@ -864,13 +864,13 @@
     variable $token
     upvar 0 $token state
     global errorInfo errorCode
     if {
        [eof $state(sock)] ||
-       [string length [fconfigure $state(sock) -error]]
+       [string length [set sockerr [fconfigure $state(sock) -error]]]
     } then {
-       Finish $token "connect failed [fconfigure $state(sock) -error]" 1
+       Finish $token "connect failed $sockerr" 1
     } else {
        set state(status) connect
        fileevent $state(sock) writable {}
     }
     return
zwischenzugsplain2012-01-13 15:54:06
Won't that fail if there's an eof on the socket?

imiell@alruccabah:~$ tclsh
% if {1 || [set a 1]} {
puts $a
}
can't read "a": no such variable
%
dkfplain2012-01-13 18:53:39
I think that can't happen; eof only happens after a read or write fails, and none has occurred at that point. (Well, maybe; don't know how TLS affects this.)
nijtmansplain2012-03-27 16:29:43
allow_comments - 1

This is already fixed by dkf (2012-01-13)
in all branches, so can be closed.
Log in (anonymous is fine) to add a comment.