vsock: a closed host-to-guest unix connection holds its host fd for 5 s, so bursty connections hit EMFILE

Open
#990 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust

Research direction

Start with UnixProxy::release() in src/devices/src/virtio/vsock/unix.rs and follow how the muxer and reaper handle ProxyRemoval::Deferred in reaper.rs. Check how the host socket fd and proxy id are used during the five-second delay, then reproduce with the provided burst test under ulimit -n 256. Done means the fd count no longer grows by one per closed connection while late guest RSTs cannot affect a reused proxy id.

Written by the indexing model from the issue text.

Description

libkrun 1.19.8 (tag v1.19.8 built from source as a static library, and Homebrew slp/krun), macOS 27.2, Apple M5 Pro. Guest kernel 6.12.109.

What happens

A port added with krun_add_vsock_port2(ctx, port, path, true) accepts host connections on path and proxies each one to the guest. Each closed connection keeps its accepted host socket open in the VMM for about 5 seconds. Under a steady stream of short connections the fd count therefore sits at about rate × 5 s. With launchd's default soft limit of 256, the VMM reaches RLIMIT_NOFILE, accept() fails with EMFILE, and new connections are refused. virtio-fs in the same VMM fails too: rm -rf on a share reported "Directory not empty".

It is a hold, not a leak. Three bursts of 200 sequential connections (connect, read one byte, close), then lsof -p <vmm> | wc -l every 0.5 s:

fds
baseline 105
after burst 1 (200 connections in 3.4 s) 305
after burst 2 (4.5 s later) 358
after burst 3 (2.2 s later) 467
0.5 s after the last connection 467
1.1 s 318
3.1 s 184
5.1 s and after 104

The count climbs by one fd per connection. It plateaus once connections start aging past 5 s, and it returns to baseline in steps, 5 s after the last connection.

Why

UnixProxy::release() returns ProxyRemoval::Deferred for every connection that reached Connected (src/devices/src/virtio/vsock/unix.rs). The muxer hands it to the reaper, which removes the proxy after TIMEOUT = 5 s (reaper.rs), and only then drops its OwnedFd. The host socket is shut down at release, but its descriptor lives on for the full deferral.

Reproduce

Guest, as the workload (python3 has AF_VSOCK):

import socket
s = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
s.bind((socket.VMADDR_CID_ANY, 1025)); s.listen(512)
while True:
    c, _ = s.accept()
    try: c.sendall(b"x")
    except OSError: pass
    c.close()

Host launcher (C, links libkrun):

krun_add_vsock_port2(ctx, 1025, "/tmp/krun-a30.sock", true);

Host driver:

pid=<vmm pid>
for burst in 1 2 3; do
  for i in $(seq 200); do nc -U /tmp/krun-a30.sock </dev/null >/dev/null; done
  echo "after burst $burst: $(lsof -p $pid | wc -l)"
done
for i in $(seq 12); do sleep 0.5; echo "idle: $(lsof -p $pid | wc -l)"; done

Run the VMM with ulimit -n 256 to see EMFILE.

Ask

One of:

  1. On release from Connected, close the host fd right away. Keep only the proxy id reserved for the 5 s, which is what the deferral is for: a late RST from the guest must not hit a reused id. The fd isn't needed for that.
  2. Or document the 5 s hold next to krun_add_vsock_port2, so embedders know to raise RLIMIT_NOFILE or to multiplex one long-lived connection.

Also seen

In the same runs, about 1 in 200 host connections got no data from the guest within 2 s; the rest completed in under 20 ms. We haven't isolated this yet, and it may be worth its own issue.

What we did meanwhile

We raised RLIMIT_NOFILE to 65536 in the VMM and carry every exec on one long-lived, multiplexed, guest-initiated connection.

Dominant language
Rust
Stars
2.8k
Forks
279
Avg merge
5d 19h
Merged PRs (30d)
48

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from libkrun/libkrun

All issues in libkrun/libkrun

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.