DeviceFarmer / adbkit

A pure Node.js client for the Android Debug Bridge.
Other
223 stars 50 forks source link

Socket is not closed after running IsInstalledCommand #526

Open verakruhliakova opened 7 months ago

verakruhliakova commented 7 months ago

Calling device.isInstalled(pkg) leads to a socket leak.

If application calls this command in a loop, eventually app crashes with EMFILE: too many open files.

Checking occupied sockets shows that they hang in CLOSED state, but never actually released by the machine.

% lsof -i -l | grep "5037"
...
node    33919      503  188u  IPv4 0x359a2e226d9499af      0t0  TCP localhost:49446->localhost:5037 (CLOSED)
node    33919      503  189u  IPv4 0x359a2e226d6259af      0t0  TCP localhost:49364->localhost:5037 (CLOSED)
node    33919      503  190u  IPv4 0x359a2e226d8d8457      0t0  TCP localhost:49501->localhost:5037 (CLOSED)
node    33919      503  191u  IPv4 0x359a2e226d539457      0t0  TCP localhost:49648->localhost:5037 (CLOSED)
node    33919      503  192u  IPv4 0x359a2e226d2fef07      0t0  TCP localhost:49479->localhost:5037 (CLOSED)
node    33919      503  193u  IPv4 0x359a2e226d59ef07      0t0  TCP localhost:49400->localhost:5037 (CLOSED)
node    33919      503  194u  IPv4 0x359a2e226d63ff07      0t0  TCP localhost:49502->localhost:5037 (CLOSED)
node    33919      503  195u  IPv4 0x359a2e226d84d457      0t0  TCP localhost:49503->localhost:5037 (CLOSED)
node    33919      503  196u  IPv4 0x359a2e226d84c9af      0t0  TCP localhost:49450->localhost:5037 (CLOSED)
node    33919      503  197u  IPv4 0x359a2e226d0a3457      0t0  TCP localhost:49546->localhost:5037 (CLOSED)
...

Environment: Node: v20.11.0 OS: MacOS 12.6.5

The reason to this issue is that command calls parser.readAscii() and never finishes reading the socket stream, which prevents socket from being closed.