Home page > OCaml > Moving out of the 70s...
Moving out of the 70s...
Tuesday 26 July 2011, by
Dear OCamlers,
The seventies were a cool time.. Progressive rock, pacifist movements and, of course, free love. However, the seventies were also the dark ages for modern computing.. and the time of POSIX..
On this note, I am sick and tired of fixing over and over the same bugs caused by corner cases and shitty aspects of the POSIX norm. The top two is the following:
SIGPIPE
A sigpipe, depending on some obscur run-time considerations, may silently terminate your program or raise an exception. If you want to be sure that you only have an exception, you need to explicitely ask for this signal to be caught.
Of course, this signal does not exist under win32 and if you try to catch it under windows, you will get an exception thrown to your face which most likely will also crash your program..
Sipgipe may be received when communicating through a network socket. Thus, any time you write a server, you need to make sure the signal is properly registered for catching. And if you do not do it, your code may work perfectly for you but crash randomly on another system. In this case, I wish you good luck to trackback the crash to this issue..
To sumarize:
- In any server-oriented program or, more generally, any program which expects an exception for sigpipe, you need to make sure the signal is registered for catching
- If you plan on porting your code to windows, you also need to not execute the registration code under windows.
Now, why is that hapenning? This is well explained in this bug report: sigpipe is also received when a program is piped to something that failed, for instance if you pipe a cat to less and the user terminates the less part.
How many OCaml programs/programmers expect their program to silently crash when receiving a sigpipe? I bet not so many.... Back to the seventies, piping programs was an essential feature of Unix. But what about the 21st century...
End of stream
Another classic of the POSIX semantics is the way that end of streams are detected. There may not be any error or exception raised when a connection has ended. Instead, the system will simply report 0 characters read when invoking Unix.read.
This means that in every place where you call this function, you need to think about what should happen if the returned number is zero and raise an appropriate exception if needed.
This also means that any failure to do so may result in a severe bug in your application.
What now?
The reason I am writing this is that although I totally understand that a "serious" programming language should adequately reflect the POSIX api — after all, skilled programmers and dinosaurs expect the POSIX behaviour — it does not seem to make sense to me that this should be the only alternative.
What about having a global flag in the Unix module that would allow to switch to a 21 century semantics? It could not fix all the shitty POSIX aspects (like having to call select to know if a socket is ready for reading or writing) but it could at least turn on some workaround, for instance making sure an exception is raised for each sigpipe or end of stream..
So, fellow OCamlers, am I the only one to struggle with those issues? Do you have other similar stories to report? Do you think that OCaml should at least have a non-default behaviour that reflects 21st century concerns and not plain old seventies hippie programming?

4 Forum messages