←back to thread

39 points mattrighetti | 4 comments | | HN request time: 0.637s | source
1. zbentley ◴[] No.45075506[source]
Not only is dlclose(3) not guaranteed to close/unload the library, close(2) isn't guaranteed to close a file!.

If the same file descriptor is open in another process (e.g. one that has it via fork(2) FD sharing either without child processes exec(2)ing or with CLOEXEC not set, or some older and esoteric abuses of fdpassing over UNIX sockets), then close(2) just decrements a refcount. The actual file isn't closed until the last holder of a reference to its file descriptor calls close(2).

This is rarely relevant, but when it comes up, it sure is wild. Common sources of confusion and pain due to this behavior are: files opened for "global library reasons" (e.g. /dev/shm buffers) in preforking servers, signalfd descriptors in preforking servers, processes that fork off and daemonize but leave their spawner around for e.g. CoW memory sharing efficiencies (looking at you, Python multiprocessing backends--the docs make it sound like they all act more or less the same, but in this regard they very much do not), libraries that usually swap STDIN/OUT/ERR file descriptors around with CLOEXEC disabled for a manually fork+exec'd child (e.g. situations where posix_spawn doesn't support needed pre-exec setup) but that are then used as part of larger applications that fork/spawn for other reasons and don't realize that file descriptor allocation/forking needs care and synchronization with the manually-fork/execing library in question: mixing forks and threads is one of those things that everyone says is a fast-track ticket to nasal demons, but that everyone also does regularly, I've found--if this describes you, be careful!

If you end up in one of those situations, suddenly invariants like "I called unlink on this path and then close(2)'d the descriptor to it, so that (maybe large) chunk of allocated space isn't taking up space on the filesystem/buffers any more" and "this externally-observable lockfile/directory is now unlocked due to its absence, now processes coordinating using that file on NFS will work as expected" no longer hold.

https://www.ibm.com/docs/en/aix/7.1.0?topic=domains-unix-dom...

I know that close(2)'s weirdness isn't a superset of dlopen(3)'s and there are different reasons for both behaviors. But it's still interesting that they "rhyme" as it were.

replies(2): >>45075721 #>>45077203 #
2. colanderman ◴[] No.45075721[source]
Terminology nit: "file descriptor" is the reference itself. "Open file description" is the thing referenced. dup(2) and fork(2) create new file descriptors which reference the same underlying open file descriptions.
replies(1): >>45083236 #
3. tux3 ◴[] No.45077203[source]
>or some older and esoteric abuses of fdpassing over UNIX sockets

One of the less well designed APIs, but as an aside it is still widely used for IPC.

4. zbentley ◴[] No.45083236[source]
Correct, sorry! My phrasing was off-the-cuff and less than precise.

File descriptor = reference/pointer-ish thing.

File description = referenced data.

close(2) = decrement refcount to description by closing descriptor; actual destruction of the file's open-ness doesn't happen until refcount == 0.