Open andrewshadura opened 3 years ago
Thanks. The code in question is https://github.com/matrix-org/synapse/blob/master/synapse/util/daemonize.py#L89-L93. As you can see it closes FDs 0-2. Is some script running synapse with another FD open? If so, why, and can it be made to not do so?
I don’t think the code in question closes them; on the contrary, it seem to be reopening /dev/null
to be available as FDs 0—2.
sorry, yes, but the act of doing so means that whatever was previously on FDs 0-2 will be closed.
From dup(2)
:
If the file descriptor newfd was previously open, it is silently closed before being reused.
FYI, I found this: https://github.com/thesharp/daemonize/blob/master/daemonize.py#L136
yeah, it's not hard to close the FDs if required. I'm just curious as to the reason it's needed.
we used to use that daemonize
library, but it has various bugs and is somewhat overcomplex. We never used that particular part of it, though the reasons are lost in the mists of time (https://github.com/matrix-org/synapse/commit/4f475c7697722e946e39e42f38f3dd03a95d8765#diff-6e21d6a61b2f6b6f2d4ce961991ba7f27e83605f927eaa4b19d2c46a975a96c1R161)
Well, in this particular case, those FDs happen to be around from debconf which ran in the same postinst script which later starts Synapse — and since it’s all non-systemd, at no point do extra FDs get closed, since that’s something synapse is I understand expected to do.
This does not, as I understand it, happen after synapse has been installed, only during the initial installation and package upgrades.
FWIW I have worked it around by not using --daemonize
as Alex suggested.
Well, in this particular case, those FDs happen to be around from debconf
so debconf opens extra FDs, and then runs synapse with the extra FDs? Or debconf is somehow changing the FDs in its parent, which are then inherited when that parent later starts synapse?
Debconf consists of a dæmon doing the UI and a shell script (sourced into postinst) talking to it. So Synapse inherits the open FDs from the shell script.
@andrewshadura do you have a link to the postinst script, ooi?
right, the postinst in question is https://salsa.debian.org/matrix-team/matrix-synapse/-/blob/debian/unstable/debian/postinst; the . /usr/share/debconf/confmodule
at the start opens an FD 3, and the #DEBHELPER#
at the end of it is expanded into commands which include invoke-rc.d --skip-systemd-native matrix-synapse start
- so that's where the extra FD is coming from.
We also have the same situation in the matrix-synapse-py3
packages distributed by matrix.org.
As debconf-devel(7) says:
- If your postinst launches a daemon, make sure you tell debconf to STOP at the end, since debconf can become a little confused about when your postinst is done otherwise.
so arguably the right fix is to add a db_stop
before the #DEBHELPER#
.
That said, I think it's probably correct that synapse should close all its open FDs when it is daemonized, and that will solve it once and for all.
Axel Beckert (@xtaran) writes in a bug report about Synapse hanging on start: