Interrupted System Call

Peter Neubauer peterneubauer2 at gmail.com
Wed Jul 23 02:01:37 PDT 2008


Hello everyone,

I've been getting the error message "Interrupted System Call" a lot 
lately, ever since I started using threads in D. It appears whenever one 
of the threads in my application creates and uses an associative array 
while another thread executes a blocking system call such as readln or 
receive on sockets. Here is an example:

=====================

import std.stdio;
import std.thread;

int readlnfunc (void* arg)
{
	readln();
	return 0;
}

void main ()
{
	(new Thread (&readlnfunc, null)).start();

	while (true) {
		int[int] aa;
		aa[0] = 0;
	}
}

=====================

I'm using dmd 1.033 on Ubuntu 8.04.

This looks like a bug to me; I don't know how to fix it or work around 
it either. Sometimes the error doesn't throw up an exception, but it 
still prevents me from using some other functions:

=====================

import std.socket;
import std.stdio;
import std.thread;

int serverfunc (void* arg)
{
	Socket listener = new TcpSocket ();
	listener.bind (new InternetAddress(1234));
	listener.listen(10);
	Socket socket = listener.accept ();
	assert (socket.isAlive);

	ubyte[512] buf;
	int bytes = socket.receive(buf);
	writefln ("Server: Received %d bytes.", bytes);

	socket.shutdown (SocketShutdown.BOTH);
	socket.close;
	listener.close;
	return 0;
}

int clientfunc (void* arg)
{
	Socket socket = new TcpSocket(new InternetAddress("127.0.0.1", 1234));
	assert (socket.isAlive);

	ubyte[512] buf;
	int bytes = socket.receive(buf);
	writefln ("Client: Received %d bytes.", bytes);

	socket.shutdown (SocketShutdown.BOTH);
	socket.close();
	return 0;
}

void main ()
{
	(new Thread (&serverfunc, null)).start();
	(new Thread (&clientfunc, null)).start();

	for (int i = 0; i < 100000; ++i) {
		int[int] aa;
		aa[0] = 0;
		Thread.yield();
	}
}

=====================

A simple server-client connection and both call receive(). Both these 
calls return Socket.ERROR, and the error disappears when I take out the 
aa in main. Optionally, if I put the aa in another extra thread and let 
the loop run before the client connects, accept() from the server will 
spit out an "Interrupted System Call".

Does anyone have any suggestions on how to fix or work around this 
problem? I couldn't find anyone else who had it.

-Peter


More information about the Digitalmars-d-learn mailing list