6 ms·
I don't really see the danger here. You can also get a signal from the OS at any time, and you have to be prepared to handle it. Do you retry your library cal
by jrockway 11y ago
I don't really see the danger here. You can also get a signal from the OS at any time, and you have to be prepared to handle it. Do you retry your library calls when they return with EINTR? (The answer appears to be no; I've seen plenty of programs exit with "Interrupted system call". That's not an error.)
If you really want something to time out, there are two options. Build into your library the ability to supply a timeout, and reliably time out; or run the operation in a separate process and kill it when the deadline is exceeded.
Go has a nice "context" idiom for carrying around cancellation and deadline information, so the chances are if you set a timeout on the context, it's likely to be obeyed because nearly every call that performs IO accepts the context and cleanly aborts when the deadline or cancellation signal arrives (presumably propagating err back to you). Though this is not perfect; disk writes do not appear to accept a context, which shows how careful you have to be when designing something to time out. ("Disk" writes can easily be network RPCs; consider NFS or FUSE.)
- MrBuddyCasino 11y ago> You can also get a signal from the OS at any time, and you have to be prepared to handle it. This is comparing Apples to ICBMs. Blocking I/O is very common, but OS signals aren't - in Java, all you need is usually a Runtime.addShutdownHook() and you're good to go. Julia Evans describes an implementation that is inherently racy, there is no way this could work 100% correct all of the time.
- JoachimSchipper 11y agoYes, EINTR handling in many scripting languages - I'm most familiar with Python - is horrifically broken. It appears that https://www.python.org/dev/peps/pep-0475/#implementation https://www.python.org/dev/peps/pep-0475/#implementation is intended to solve this issue... for Python 3.5.
- mikeash 11y agoYou don't have to be prepared to handle signals most of the time. When a signal arrives, execution gets interrupted, the signal is handled, then execution resumes transparently. The code doesn't even know anything happened. This is vastly different from throwing an exception in the middle of arbitrary, unexpecting code. Failing to handle EINTR is not good, but at least it fails cleanly. This is still way, way better than throwing an exception in random places, potentially in code that "can't throw," and leaving a trail of corruption behind.