www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - linux system calls are canceled by GC

reply shinichiro.h <shinichiro.hamaji gmail.com> writes:
Hi D guys,

The following program

import std.stream;
import std.cstream;
import std.thread;

class ReaderThread : Thread {
    override int run() {
        char[] line = din.readLine();
        dout.writeLine(line);
        return 0;
    }
}

void main() {
    ReaderThread rt = new ReaderThread();
    rt.start();
    for (int i = 0; i < 100000; i++) {
        new Object;
    }
    rt.wait();
}

works as intended on Windows (suppose a scenario in which a user may input a
line after the for-loop):

1. Start ReaderThread
2. 100000 allocations
3. a user input a line
4. show the line and finish ReaderThread
5. the program finishes

but doesn't work on Linux

1. Start ReaderThread
2. 100000 allocations
3. during the allocations, GC is invoked and SIGUSR1 is issued to stop the world
4. the SIGUSR1 makes read system call fail (errno=EINTR)
5. the program outputs empty line and finishes without waiting the user's input.

this issue is serious when we are writing network applications (synchronous
accept(2) or read(2) fail often). The following super short patch will fix the
problem:

--- /usr/local/dmd/src/phobos/std/thread.d      2008-04-28 06:00:52.000000000
+0900
+++ thread.d    2008-07-22 03:21:11.000000000 +0900
   -1044,6 +1044,7   
        if (result)
            goto Lfail;
        sigact.sa_handler = &pauseHandler;
+       sigact.sa_flags = 0x10000000u  /* SA_RESTART */;
        result = sigaction(SIGUSR1, &sigact, null);
        if (result)
            goto Lfail;

you may want to put SA_RESTART in somewhere else (supposedly,
std.c.linux.linux?) though. I believe tango doesn't have this problem since its
Thread.d seems to set SA_RESTART. I hope this patch is accepted in near future.

I'm using dmd-2.014.

Thanks,
Jul 22 2008
parent Gide Nwawudu <gide btinternet.com> writes:
On Tue, 22 Jul 2008 09:50:06 -0400, shinichiro.h
<shinichiro.hamaji gmail.com> wrote:

Hi D guys,

The following program
[snip] Logged as bug 2242. http://d.puremagic.com/issues/show_bug.cgi?id=2242
Jul 24 2008