Adrián Pérez is a user on mastodon.social. You can follow them or interact with them if you have an account anywhere in the fediverse. If you don't, you can sign up here.
Adrián Pérez @aperezdc

Programming quiz:

1. Create a GMainLoop.

2. Use socketpair(), wrap “server” side descriptor into a GSocket. Write some data.

4. Spawn a new process, passing the “client” side descriptor. This process also creates a GMainLoop and wraps the descriptor into a GSocket.

3. Server runs the main loop.

5. Client create sa GSource for the socket, to get async notifications on “data ready”, and starts its main loop.

The data written by the server process never triggers “data ready”. Where's my data?

· Web · 0 · 0

Of course, writing the data is successful and produces no errors. Theoretically the data should be buffered somewhere by the kernel, as the socket pair is expected to behave like a FIFO pipe. Any ideas?

I would really like to send an initial message over the socketpair which the spawned process would read right away on startup.

After two days banging my head with this, now I had a moment of enlightenment: the data remains in limbo, but it does not trigger the GSource because the GSocket/GSource has been created in the child process *after* the data has been already accepted by the kernel, and therefore the “data available” event never gets triggered.

What the child process can do is use g_socket_get_available_bytes() as the first thing right after creating the GSocket, and manually invoke the “data ready” callback.

@aperezdc Another option is to write a "child-ready" byte to the socket and the server blocks until that notification from the child.

Also, futex in shared memory, semaphore, etc to synchronize the startup.

@hergertme @aperezdc The wait-for-child approach is probably more robust than what I'm doing right now but this is for something WebKit-related so I'll have to see whether I can accommodate to block there, as it has its own internal IPC paraphernalia which I cannot use but wouldn't want to break.

@aperezdc @hergertme Checking whether there's data in the socket right after creation is working now, so I'll leave it like for the moment. I don't want to break things now that it has been two days banging my head against this. Also: I should have guessed earlier, I feel silly for not noticing 😅