Permalink
Browse files

[chiralml] Work around a severe SML/NJ bug.

  • Loading branch information...
Jacob Potter
Jacob Potter committed Sep 2, 2010
1 parent 571329b commit f964f431698b3f46dc0a12ed1af2ee55a9724b8f
Showing with 20 additions and 3 deletions.
  1. +20 −3 chiralml/socket.sml
View
@@ -40,13 +40,30 @@ functor ChiralSocketFn (T: THREAD) :> CHIRAL_SOCKET
val sameAddr = S.sameAddr
val familyOfAddr = S.familyOfAddr
+ (* Work around an SML/NJ bug.
+ *
+ * In versions of SML/NJ through 110.72, Socket.acceptNB has a problem:
+ * it returns a socket that SML/NJ thinks is already nonblocking, but
+ * which the OS thinks is blocking. Therefore, future NB calls will
+ * actually block. Performing a single "blocking" call, even writing
+ * zero bytes, will fix the situation.
+ *)
+ val nullSlice = Word8VectorSlice.full (Byte.stringToBytes "")
+ fun acceptNB s = let
+ val s' = S.acceptNB s
+ val () = case s' of
+ NONE => ()
+ | SOME (sock, _) => ignore (S.sendVec (sock, nullSlice))
+ in
+ s'
+ end
+
+ fun accept s = wrap (acceptNB, s, s, C.BLOCK_RD)
+
val bind = S.bind
val listen = S.listen
- val acceptNB = S.acceptNB
val connectNB = S.connectNB
- fun accept s = wrap (S.acceptNB, s, s, C.BLOCK_RD)
-
(* Connect doesn't use wrap, because unlike all the other functions, we
* don't want to retry the connect once it's writeable; we just want to
* return. *)

0 comments on commit f964f43

Please sign in to comment.