Phobos does not look to be EINTR-aware. Many posix functions used in phobos can be potentially interrupted by delivering a signal.
Examples are posix write and read used in std.file.write and std.file.read.
It's easy to make EINTR-aware wrappers for such interruptible functions.
import core.stdc.errno;
import std.traits;
template deintr(alias func, Args...) if (isFunction!func && isIntegral!(ReturnType!func))
{
ReturnType!func deintr(Parameters!func args) {
ReturnType!func result;
do {
result = func(args);
} while(result == -1 && .errno == EINTR);
return result;
}
}
then use it like this:
import core.sys.posix.unistd;
alias dwrite = deintr!(.write);
dwrite(fd, buf.ptr, buf.length);
The question is if it's really needed. What is the current phobos policy on signals?
Comment #1 by freeslave93 — 2017-04-05T19:20:46Z
It turned out that the problem can appear even if user does not do signal handling himself. It's enough to just do multithreading, so signals used by GC can interrupt syscall in other thread.
Whether the hack is applied or not, the behavior of functions will need to be documented. If hack is applied, it should say about possible looping, if it's not applied it should say that interruption is considered error for these functions.
There's also updated version of the hack:
template deintr(alias func)
if (isFunction!func && isIntegral!(ReturnType!func) &&
isSigned!(ReturnType!func) && functionLinkage!func == "C")
{
ReturnType!func deintr(Args...)(Parameters!func args, Args varArgs)
{
ReturnType!func result;
do
{
result = func(args, varArgs);
}
while(result == -1 && .errno == EINTR);
return result;
}
}