socket bug in mlton 20000906

Stephen Weeks MLton@sourcelight.com
Thu, 7 Jun 2001 17:39:09 -0700


--ntbZS2vb9t
Content-Type: text/plain; charset=us-ascii
Content-Description: message body text
Content-Transfer-Encoding: 7bit


> Someone submitted an echo client/server solution to my shootout,

Cool.

> and when I had problems running it due to a run-time assertion on
> setsockopt, I found what looks like a bug in mlton-lib.c.
> 
> The setsockopt(SO_REUSEADDR) should be located between calls to
> socket() and bind() on the listen socket, not after a new client
> socket is accept()'ed.
> 
> I imagine you may have already found this one, but enclosed is a patch
> in the off chance you have not.

I found the problem the first time I tried to run a server on a RedHat 7.1
machine, which was just a week or so ago.  I didn't have time to find the fix
though -- I just commented out the setsockopt.  Thanks for the fix.

Just for fun, I compiled with our latest internal version of MLton and got about
a 10% speedup.  There will be a release RSN and I'll make sure and let you know
about it.

I would like to change the program to the following, which includes calls to
TextIO.flushOut after the calls to TextIO.output on the sockets.  This is
because the new version of MLton buffers sockets (unlike the version you have,
but like stdio).



--ntbZS2vb9t
Content-Type: application/octet-stream
Content-Disposition: attachment;
	filename="echo.mlton"
Content-Transfer-Encoding: base64

KCogLSotIG1vZGU6IHNtbCAtKi0KICogJElkOiBlY2hvLm1sdG9uLHYgMS4xIDIwMDEvMDYvMDcg
MjI6MzI6NDkgZG91ZyBFeHAgJAogKiBodHRwOi8vd3d3LmJhZ2xleS5vcmcvfmRvdWcvc2hvb3Rv
dXQvCiAqIGZyb20gVG9tIDcKICopCgpleGNlcHRpb24gRXJyb3Igb2Ygc3RyaW5nCgpvcGVuIE1M
dG9uLlNvY2tldAogICAgCnZhbCBkYXRhID0gIkhlbGxvIHRoZXJlIHNhaWxvclxuIgogICAgCnZh
bCBudW0gPSBjYXNlIENvbW1hbmRMaW5lLmFyZ3VtZW50cygpIG9mCiAgICBuaWwgPT4gMQogIHwg
bjo6XyA9PiB2YWxPZiAoSW50LmZyb21TdHJpbmcgbikKCnZhbCAocG9ydCwgbGlzdGVuZXIpID0g
bGlzdGVuICgpCiAgICBoYW5kbGUgXyA9PiAKICAgICAgICByYWlzZSBFcnJvciAoIkNhbid0IGxp
c3Rlbi4uLlxuIikKCmZ1biBzZXJ2ZXIgKCkgPQogICAgbGV0IHZhbCAoXywgXywgaW5zLCBvdXRz
KSA9IGFjY2VwdCBsaXN0ZW5lcgogICAgICAgIGZ1biBzIGIgPSAKICAgICAgICAgICAgY2FzZSBU
ZXh0SU8uaW5wdXRMaW5lIGlucyBvZgogICAgICAgICAgICAgICAgIiIgPT4gbGV0IGluCiAgICAg
ICAgICAgICAgICAgICAgICAgICAgUG9zaXguUHJvY2Vzcy53YWl0ICgpOwogICAgICAgICAgICAg
ICAgICAgICAgICAgIHByaW50IChjb25jYXQgWyJzZXJ2ZXIgcHJvY2Vzc2VkICIsCgkJCQkJIElu
dC50b1N0cmluZyBiLAoJCQkJCSAiIGJ5dGVzXG4iXSkKICAgICAgICAgICAgICAgICAgICAgIGVu
ZAogICAgICAgICAgICAgIHwgaSA9PiAgbGV0IGluIAogICAgICAgICAgICAgICAgICAgICAgICAg
IFRleHRJTy5vdXRwdXQob3V0cywgaSk7CgkJCSAgVGV4dElPLmZsdXNoT3V0IG91dHM7CiAgICAg
ICAgICAgICAgICAgICAgICAgICAgcyAoYiArIDE5KQogICAgICAgICAgICAgICAgICAgICAgZW5k
CiAgICBpbiBzIDAKICAgIGVuZAoKZnVuIGNsaWVudCAoKSA9CiAgICBsZXQKICAgICAgICB2YWwg
KGlucywgb3V0cykgPSBjb25uZWN0ICgiMTI3LjAuMC4xIiwgcG9ydCkKICAgICAgICBmdW4gYyAw
ID0gbGV0IGluCiAgICAgICAgICAgICAgICAgICAgICBUZXh0SU8uY2xvc2VPdXQgb3V0czsKICAg
ICAgICAgICAgICAgICAgICAgIFRleHRJTy5jbG9zZUluIGlucwogICAgICAgICAgICAgICAgICBl
bmQKICAgICAgICAgIHwgYyBuID0gbGV0IGluCiAgICAgICAgICAgICAgICAgICAgICBUZXh0SU8u
b3V0cHV0KG91dHMsIGRhdGEpOwoJCSAgICAgIFRleHRJTy5mbHVzaE91dCBvdXRzOwogICAgICAg
ICAgICAgICAgICAgICAgVGV4dElPLmlucHV0TGluZSBpbnMgPSBkYXRhCiAgICAgICAgICAgICAg
ICAgICAgICAgICAgb3JlbHNlIHJhaXNlIEVycm9yICJEaWRuJ3QgcmVjZWl2ZSB0aGUgc2FtZSBk
YXRhIjsKICAgICAgICAgICAgICAgICAgICAgIGMgKG4gLSAxKQogICAgICAgICAgICAgICAgICBl
bmQKICAgIGluCiAgICAgICAgYyBudW0KICAgIGVuZAoKdmFsIF8gPSBjYXNlIFBvc2l4LlByb2Nl
c3MuZm9yayAoKSBvZgogICAgU09NRSBwaWQgPT4gc2VydmVyICgpCiAgfCBOT05FID0+IGNsaWVu
dCAoKQo=

--ntbZS2vb9t--