Lachee / unity-named-pipes

A native named pipe wrapper for Unity3D
MIT License
60 stars 7 forks source link

Unix implementation sockets are not closed when the socket path failed to connect or does not exist #13

Open mdsitton opened 10 months ago

mdsitton commented 10 months ago

This causes specifically with the https://github.com/Lachee/discord-rpc-csharp library for games built with unity to crash on linux as they reach the max number of open file descriptors because of all of the dangling sockets:

I built a simple C++ program to reproduce this behavior https://gist.github.com/mdsitton/95fc0040d01cdf198961cb72255c234a:

NamedPipe 24436 deck  154u     unix 0x0000000032901640      0t0 1758654 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  155u     unix 0x00000000d5c3a1e9      0t0 1758660 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  156u     unix 0x00000000683a598f      0t0 1758661 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  157u     unix 0x00000000c55ccb76      0t0 1758662 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  158u     unix 0x00000000014525f2      0t0 1758663 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  159u     unix 0x00000000d50b286d      0t0 1758664 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  160u     unix 0x00000000e67ac18d      0t0 1758665 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  161u     unix 0x00000000b56c7489      0t0 1758666 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  162u     unix 0x000000005ec439e0      0t0 1758667 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  163u     unix 0x0000000048697d99      0t0 1758668 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  164u     unix 0x00000000134ceea6      0t0 1758669 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  165u     unix 0x00000000d28c99e8      0t0 1758673 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  166u     unix 0x0000000077915d8f      0t0 1758674 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  167u     unix 0x000000005b9e8cf4      0t0 1758675 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  168u     unix 0x00000000427a36ff      0t0 1758676 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  169u     unix 0x00000000a20791de      0t0 1758677 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  170u     unix 0x00000000fea049c2      0t0 1758678 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  171u     unix 0x0000000096185ac3      0t0 1758679 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  172u     unix 0x00000000095ce08f      0t0 1758680 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  173u     unix 0x000000008790cba8      0t0 1758681 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  174u     unix 0x000000000c4dd30c      0t0 1758682 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  175u     unix 0x00000000d8bd7810      0t0 1758688 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  176u     unix 0x00000000b0c390bf      0t0 1758689 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  177u     unix 0x0000000073576f72      0t0 1758690 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  178u     unix 0x00000000d50b8cda      0t0 1758691 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  179u     unix 0x0000000078206672      0t0 1758692 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  180u     unix 0x00000000ea085407      0t0 1758693 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  181u     unix 0x00000000720ba526      0t0 1758694 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  182u     unix 0x00000000f357ca4e      0t0 1758695 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  183u     unix 0x00000000784fe663      0t0 1758696 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  184u     unix 0x000000002008e488      0t0 1758697 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  185u     unix 0x00000000390b7635      0t0 1758702 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  186u     unix 0x00000000e8415e72      0t0 1758703 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  187u     unix 0x00000000fcddf6dd      0t0 1758704 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  188u     unix 0x0000000062727928      0t0 1758705 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  189u     unix 0x00000000fa2d3c4a      0t0 1758706 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  190u     unix 0x00000000a7262f67      0t0 1758707 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  191u     unix 0x00000000cac478ad      0t0 1758708 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  192u     unix 0x000000009f51dda5      0t0 1758709 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  193u     unix 0x0000000070f71bcb      0t0 1758710 type=STREAM (UNCONNECTED)
NamedPipe 24436 deck  194u     unix 0x00000000f631da32      0t0 1758711 type=STREAM (UNCONNECTED)
(deck@steamdeck ~)$ lsof -p 24436 | grep -c STREAM
270

The issue occurs here: https://github.com/Lachee/unity-named-pipes/blob/master/UnityNamedPipe.Native/connection_unix.cpp#L97

This happens because isOpened is only set to true if a connection is made to the input path, but we still have a socket which doesn't get closed properly.

mdsitton commented 10 months ago

I have a fork over here which fixes this issue but it's not exactly setup as something to merge back into this repo(i'm trying to get a game update out on a tight time window so i just was focusing on making it work) as i changed quite a few things with the native layer to fix issues i was having when building the library(for whatever reason i was getting segmentation faults when trying to call close on the file descriptor until i renamed all of the functions so that the Close() in this code was differently named from the platform one. So i ended up renaming all functions to be PascalCase for consistency): https://github.com/mdsitton/unity-named-pipes

Additionally i've added an implementation for windows/unix to check if a socket/named pipe exists before we open it to reduce the number of sockets being used overall if we don't need to open a socket.

This also contains my example C++ application as linked above.

Lachee commented 10 months ago

Awesome, thanks for your work.

I'll have a look in the next few days and port your fixes into the project :3

kobayashi-alec commented 1 month ago

Hey @Lachee,

Awesome, thanks for your work. I'll have a look in the next few days and port your fixes into the project :3

Did you ever find the time to fix this?