On Fri, 08 Mar 2019 15:10:39 GMT, David Eckardt wrote:

Hello,

I am writing a network client program using Vibe on Linux. The program needs to log out from the server on shutdown, when SIGINT is raised (like pressing Ctrl+C). In order to do so, I tried to use createFileDescriptorEvent with a Linux signalfd.
What happens is that FileDescriptorEvent.wait() returns immediately rather than waiting for the signalfd to be ready for reading. I traced it down to a recv(MSG_PEEK) call in eventcore. This call fails with ENOTSOCK.
My question now is, in order to understand how non-socket file descriptors could be supported: What is the purpose of this call?

Thanks,
David

createFileDescriptorEvent is currently limited to sockets or socket-like file descriptors by just blindly using eventcore's socket mechanism. This could be extended in the future, but for the time being I'd recommend to instead drop one level deeper and use eventcore directly, which already wraps signalfd.

To ensure proper interaction with the task switching logic, a yieldLock should be used in conjunction with starting a task within the callback (except if the shutdown procedure does not require I/O or yielding execution for another reason):

/+ dub.sdl:
	dependency "vibe-core" version="~>1.6"
+/
import vibe.core.core;
import vibe.core.log;

import core.time;
import core.sys.posix.signal;
import eventcore.core;

void main()
{
	bool do_shutdown;

	auto t = runTask({
		while (!do_shutdown) {
			sleep(500.msecs);
			logInfo("Still alive...");
		}
		sleep(500.msecs);
	});

	eventDriver.signals.listen(SIGINT, (id, status, sig) {
		auto l = yieldLock();

		eventDriver.signals.releaseRef(id);
	
		logInfo("Caught SIGINT!");

		runTask({
			logInfo("Initiating shutdown procedure...");
			do_shutdown = true;
			t.join();
			logInfo("Shut down!");
			exitEventLoop();
		});
	});

	runApplication();
}

At some point the signalfd functionality should get integrated properly into vibe-core, but until then this should work.