Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

unix: reimplement EMFILE trick #315

Closed
bnoordhuis opened this Issue · 2 comments

2 participants

@bnoordhuis

Stash away an open file descriptor. When EMFILE is returned by e.g. accept(), close the spare and retry the operation, then close the new file descriptor. (Note: not foolproof in multi-threaded environments.)

This is good because it signals to peers that we're overloaded / stops connections from rotting away in the listen backlog. It also mitigates issues like joyent/node#2529.

@bnoordhuis bnoordhuis was assigned
@dvv

+1

@bnoordhuis bnoordhuis closed this issue from a commit
@bnoordhuis bnoordhuis unix: reimplement accept() EMFILE trick
Implement a best effort approach to mitigating accept() EMFILE errors.

We have a spare file descriptor stashed away that we close to get below
the EMFILE limit. Next, we accept all pending connections and close them
immediately to signal the clients that we're overloaded - and we are, but
we still keep on trucking.

There is one caveat: it's not reliable in a multi-threaded environment.
The file descriptor limit is per process. Our party trick fails if another
thread opens a file or creates a socket in the time window between us
calling close() and accept().

Fixes #315.
de8fc7b
@bnoordhuis bnoordhuis referenced this issue from a commit
@bnoordhuis bnoordhuis unix: reimplement accept() EMFILE trick
Implement a best effort approach to mitigating accept() EMFILE errors.

We have a spare file descriptor stashed away that we close to get below
the EMFILE limit. Next, we accept all pending connections and close them
immediately to signal the clients that we're overloaded - and we are, but
we still keep on trucking.

There is one caveat: it's not reliable in a multi-threaded environment.
The file descriptor limit is per process. Our party trick fails if another
thread opens a file or creates a socket in the time window between us
calling close() and accept().

Fixes #315.
4f5c8da
@bnoordhuis

Fixed in 4f5c8da.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.