Skip to content

Support async NamedPipeServerStream.WaitForConnection for synchronous pipes #63536

@GSPP

Description

@GSPP

Motivating use case: My codebase is using synchronous pipe IO for IPC. The application spends a lot of CPU in the IPC mechanism, and synchronous IO uses less CPU than async IO. Thread count is not an issue, and synchronous code is simpler than async code. Sync IO is an appropriate solution.

I only need async for the WaitForConnection call because the child process might die and never connect. A timeout is not possible. Instead, I'm listening for process exit to terminate the pipe.

A synchronous WaitForConnection call is really hard to break. For example, closing the pipe handle (Dispose) does not unblock that call... Probably, CancelSynchronousIo could do it, but I read in another issue that this is hard to implement.

WaitForConnectionAsync works on a synchronous pipe, but it simply delegates the sync call to the thread-pool with no ability to cancel. That call will hang forever. I wonder if this implicit async-over-sync design might be a foot gun for people.

Currently, the only workaround that I know of is to create a dummy client pipe and connect to the server to break the WaitForConnection call. This scheme is not the nicest solution in the world, and I wonder if there are circumstances where it would break (security?).

What would it take to support this? Does the Win32 API not support overlapped IO on pipes if the pipe mode is synchronous? I could not find such a statement in the documentation. But if that is the case, SetNamedPipeHandleMode can temporarily switch the mode to async. Is this feasible to support? This behavior would then be transparent to callers of WaitForConnectionAsync.

An alternative solution is to make IsAsync settable. ReadMode already is settable, so the infrastructure and precedence for calling SetNamedPipeHandleMode are there.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions