From: Ivan Shvedunov
Subject: SBCL and/or Araneida socket handling problem
Date: 
Message-ID: <33namgF41qrdiU1@individual.net>
Happy New Year!

I have the following problem:
My app uses a serve-event based central event loop, and provides a 
simple Web-based UI via Araneida using threaded-http-listener. Sometimes 
(perhaps when TCP connection is interrupted while HTTP response is being 
written to a socket) I get a strange error:

Caught error: failure in Unix lseek() on #<FILE-STREAM for "a constant 
string" {9F68E71}>:
                 Bad file descriptor

(the full backtrace is at the bottom of message).
After that, perhaps a handler for invalid descriptor is left in 
SB-IMPL::*DESCRIPTOR-HANDLERS* and my app's event thread is thrown into 
debugger:

   NIL have bad file descriptors.
...
0: (SB-IMPL::HANDLER-DESCRIPTORS-ERROR 0)[:EXTERNAL]
1: (SB-IMPL::SUB-SERVE-EVENT 2 0 204000)[:EXTERNAL]

(I cannot easily handle that condition as it's SIMPLE-ERROR)

I'm not quite sure as I'm by no means an experienced Lisper, but seems 
like the problem is probably caused by attempted FILE-POSITION on a 
socket stream in SB-SYS:OUTPUT-RAW-BYTES (src/code/fd-stream.lisp in 
SBCL sources). Probably trying to call lseek on socket is not a good 
idea at all, and it seems that fd handler for socket somehow is not 
removed when this call fails.

How can I workaround this problem?

Thanks in advance,
   Ivan

----
Backtrace:

Caught error: failure in Unix lseek() on #<FILE-STREAM for "a constant 
string" {9F68E71}>:
                 Bad file descriptor
0: ("hairy arg processor for top level local call SB!DEBUG:BACKTRACE"
     15
     #<SYNONYM-STREAM :SYMBOL SB-SYS:*STDOUT* {5010149}>)
1: (ARANEIDA::HANDLER-DEBUGGER-HOOK
     #<SB-INT:SIMPLE-STREAM-ERROR {90FEC29}>
     #<unavailable argument>)
2: (INVOKE-DEBUGGER 1 #<SB-INT:SIMPLE-STREAM-ERROR {90FEC29}>)[:EXTERNAL]
3: (ERROR 7 SB-INT:SIMPLE-STREAM-ERROR)[:EXTERNAL]
4: (SB-IMPL::SIMPLE-STREAM-PERROR
     3
     "failure in Unix lseek() on ~S"
     #<FILE-STREAM for "a constant string" {9F68E71}>
     9)[:EXTERNAL]
5: ("FLET #:WITHOUT-INTERRUPTS-BODY-2686")
6: ("hairy arg processor for top level local call 
SB!IMPL::FD-STREAM-FILE-POSITION"
     #<FILE-STREAM for "a constant string" {9F68E71}>
     NIL)
7: ("hairy arg processor for top level local call FILE-POSITION"
     #<FILE-STREAM for "a constant string" {9F68E71}>
     NIL)
: ("hairy arg processor for top level local call SB!SYS:OUTPUT-RAW-BYTES"
     #<FILE-STREAM for "a constant string" {9F68E71}>
     "HTTP/1.0 "
     0
     9)
9: (SB-IMPL::FD-SOUT
     4
     #<FILE-STREAM for "a constant string" {9F68E71}>
     "HTTP/1.0 "
     0
     9)[:EXTERNAL]
10: (SB-IMPL::%WRITE-STRING
      4
      "HTTP/1.0 "
      #<FILE-STREAM for "a constant string" {9F68E71}>
      0
      9)[:EXTERNAL]
11: ("varargs entry for top level local call WRITE-STRING"
      "HTTP/1.0 "
      #<FILE-STREAM for "a constant string" {9F68E71}>
      :START
      0
      :END
      NIL)
12: (SB-FORMAT::INTERPRET-DIRECTIVE-LIST
      4
      #<FILE-STREAM for "a constant string" {9F68E71}>
      ("HTTP/1.0 " #<~D> " " #<~A> "
Date: "
       #<~A> "
Server: "
       #<~A> "
Connection: close"
       #<~%>)
      (200 "OK" "Sat, 01 Jan 2005 09:08:16 GMT" "Araneida/0.9")
      (200 "OK" "Sat, 01 Jan 2005 09:08:16 GMT" "Araneida/0.9"))[:EXTERNAL]
13: ("hairy arg processor for top level local call SB!FORMAT::%FORMAT"
      #<FILE-STREAM for "a constant string" {9F68E71}>
      "HTTP/1.0 ~D ~A
Date: ~A
Server: ~A
Connection: close~%"
      (200 "OK" "Sat, 01 Jan 2005 09:08:16 GMT" "Araneida/0.9")
      (200 "OK" "Sat, 01 Jan 2005 09:08:16 GMT" "Araneida/0.9"))
14: (FORMAT
      6
      #<FILE-STREAM for "a constant string" {9F68E71}>
      "HTTP/1.0 ~D ~A
Date: ~A
Server: ~A
Connection: close~%")[:EXTERNAL]