diff options
| author | dgp@users.sourceforge.net <dgp> | 2014-06-16 13:38:26 (GMT) |
|---|---|---|
| committer | dgp@users.sourceforge.net <dgp> | 2014-06-16 13:38:26 (GMT) |
| commit | 4c5ae05bd9e1728aed97fe3bb577e55e4f2a48fe (patch) | |
| tree | 35dbcc58f933c55142e844a98c74d426d75e16da | |
| parent | e07fc4d4d704c4a31363c6041ae0e57386e5fa82 (diff) | |
| parent | 788df0af046b7097c8932c745276573fed6c47ef (diff) | |
| download | tcl-4c5ae05bd9e1728aed97fe3bb577e55e4f2a48fe.zip tcl-4c5ae05bd9e1728aed97fe3bb577e55e4f2a48fe.tar.gz tcl-4c5ae05bd9e1728aed97fe3bb577e55e4f2a48fe.tar.bz2 | |
[1758a0b603] socket_*-2.13 : Workaround the broken select() in some Linux
kernels that fails to report a writable state on a socket when an error
condition (or remote close) is present.
| -rw-r--r-- | unix/tclUnixSock.c | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/unix/tclUnixSock.c b/unix/tclUnixSock.c index a9323c4..96700ce 100644 --- a/unix/tclUnixSock.c +++ b/unix/tclUnixSock.c @@ -55,6 +55,8 @@ struct TcpState { TcpFdList fds; /* The file descriptors of the sockets. */ int flags; /* ORed combination of the bitfields defined * below. */ + int interest; /* Event types of interest */ + /* * Only needed for server sockets */ @@ -134,6 +136,7 @@ static int TcpOutputProc(ClientData instanceData, const char *buf, int toWrite, int *errorCode); static void TcpWatchProc(ClientData instanceData, int mask); static int WaitForConnect(TcpState *statePtr, int *errorCodePtr); +static void WrapNotify(ClientData clientData, int mask); /* * This structure describes the channel type structure for TCP socket @@ -906,6 +909,35 @@ TcpGetOptionProc( */ static void +WrapNotify( + ClientData clientData, + int mask) +{ + TcpState *statePtr = (TcpState *) clientData; + int newmask = mask & statePtr->interest; + + if (newmask == 0) { + /* + * There was no overlap between the states the channel is + * interested in notifications for, and the states that are + * reported present on the file descriptor by select(). The + * only way that can happen is when the channel is interested + * in a writable condition, and only a readable state is reported + * present (see TcpWatchProc() below). In that case, signal back + * to the caller the writable state, which is really an error + * condition. As an extra check on that assumption, check for + * a non-zero value of errno before reporting an artificial + * writable state. + */ + if (errno == 0) { + return; + } + newmask = TCL_WRITABLE; + } + Tcl_NotifyChannel(statePtr->channel, newmask); +} + +static void TcpWatchProc( ClientData instanceData, /* The socket state. */ int mask) /* Events of interest; an OR-ed combination of @@ -928,8 +960,30 @@ TcpWatchProc( * need to cache this request until the connection has succeeded. */ statePtr->filehandlers = mask; } else if (mask) { - Tcl_CreateFileHandler(statePtr->fds.fd, mask, - (Tcl_FileProc *) Tcl_NotifyChannel, statePtr->channel); + + /* + * Whether it is a bug or feature or otherwise, it is a fact + * of life that on at least some Linux kernels select() fails + * to report that a socket file descriptor is writable when + * the other end of the socket is closed. This is in contrast + * to the guarantees Tcl makes that its channels become + * writable and fire writable events on an error conditon. + * This has caused a leak of file descriptors in a state of + * background flushing. See Tcl ticket 1758a0b603. + * + * As a workaround, when our caller indicates an interest in + * writable notifications, we must tell the notifier built + * around select() that we are interested in the readable state + * of the file descriptor as well, as that is the only reliable + * means to get notified of error conditions. Then it is the + * task of WrapNotify() above to untangle the meaning of these + * channel states and report the chan events as best it can. + * We save a copy of the mask passed in to assist with that. + */ + + statePtr->interest = mask; + Tcl_CreateFileHandler(statePtr->fds.fd, mask|TCL_READABLE, + (Tcl_FileProc *) WrapNotify, statePtr); } else { Tcl_DeleteFileHandler(statePtr->fds.fd); } |
