metalbear-co / mirrord

Connect your local process and your cloud environment, and run local code in cloud conditions.
https://mirrord.dev
MIT License
3.84k stars 107 forks source link

Respect socket's non blocking connect #1898

Open aviramha opened 1 year ago

aviramha commented 1 year ago

Current implementation of the connect detour always waits for the other side to accept before it returns, while most applications nowadays set the socket to non blocking, then connect should immediately return with EINPROGRESS error code, indicating that the connect is in progress. We can improve our implementation to check if socket is non blocking, and if it is, we setup the mirror listen socket, but don't accept until agent returns the result, to reproduce similar behavior (as much as possible).

Razz4780 commented 6 months ago

It would be a bit tricky to implement it this way. The usual flow is to call connect and expect EINPROGRESS, then use poll to wait for result. getsockopt can also be used to get connected/error state of the socket. We'd have to asynchronously let the layer know that the listener is ready (which doesn't fit well into our request-response model and lack of background threads :X)

A simpler solution would be to follow the usual TCP proxy pattern - eagerly accept connection, kill it if we fail to connect to the real peer

aviramha commented 6 months ago

Why is it tricky though? Layer sends ConnectRequest intProxy responds ConnectInProgress layer continues Intproxy accepts connection only after connection is really made on the remote side, if not returns error

Razz4780 commented 6 months ago

We can improve our implementation to check if socket is non blocking, and if it is, we setup the mirror listen socket, but don't accept until agent returns the result

Aaaah sorry. Yes. This should work