lwt icon indicating copy to clipboard operation
lwt copied to clipboard

poll/read: case for Lwt_unix raw job control

Open dsheets opened this issue 11 years ago • 2 comments
trafficstars

src/unix/lwt_unix_unix.c makes the assumption that poll(&pollfd,1,0) will not block and will always return immediately. Indeed, this is what the man page for poll states.

Unfortunately, poll waits for a response from the filesystem which, if unresponsive, will never arrive. In this case, poll will block the process.

I do not see a means to read an fd without first polling for readability with Lwt_unix. Is there a way? I also do not see any way to get an 'a Lwt_unix.job for use with Lwt_unix.run_job. I would really like to have a way to attempt a read (in another thread) without first polling in the main thread.

dsheets avatar Jan 23 '14 16:01 dsheets

Hmm, I assumed poll would return immediately saying that the fd is readable in case the fd is a file.

This call is not strictly necessary: it is to handle the case when a file descriptor supporting non-blocking is used setting the nonblock flag, to give the user a chance to cancel the operation. For instance for stdin/out/err. So I guess we could remove it.

Jobs have to be created from the C code. If you want the job used for Lwt_unix.read, you can define the external in your code:

external read_job : Unix.file_descr -> string -> int -> int -> int job = "lwt_unix_read_job"

The name/signature is not going to change.

ghost avatar Jan 23 '14 17:01 ghost

Yes, I guess I don't understand why blocking fds bother to call poll at all instead of just spawning a job and blocking inside that. poll can block on files if the file is, e.g. remote over NFS or a FUSE filesystem.

dsheets avatar Jan 23 '14 17:01 dsheets