From c6bd1eea71b08372ec6d3a60b5203110c9bd66b2 Mon Sep 17 00:00:00 2001 From: Luflosi Date: Tue, 25 Jan 2022 01:52:42 +0100 Subject: [PATCH] nixos/tor: fix tor getting killed by systemd when shuttding down Tor waits ShutdownWaitLength seconds (30s by default) before actually shutting down. Since the systemd timeout is also set to 30 seconds, this results in a race condition that ends up killing Tor most of the time on my machine during shutdown. To fix this, add the ShutdownWaitLength setting and tell systemd to wait 30 seconds more than that. Arch Linux also has `TimeoutSec` set to 60 seconds: https://github.com/archlinux/svntogit-community/blob/6df716fe195984624d475b79b2cd9d290452c346/trunk/tor.service. --- nixos/modules/services/security/tor.nix | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/nixos/modules/services/security/tor.nix b/nixos/modules/services/security/tor.nix index f3ed1d160eed..cafb44e12429 100644 --- a/nixos/modules/services/security/tor.nix +++ b/nixos/modules/services/security/tor.nix @@ -794,6 +794,11 @@ in }; })); }; + options.ShutdownWaitLength = mkOption { + type = types.int; + default = 30; + description = descriptionGeneric "ShutdownWaitLength"; + }; options.SocksPolicy = optionStrings "SocksPolicy" // { example = ["accept *:*"]; }; @@ -977,7 +982,7 @@ in ExecStart = "${cfg.package}/bin/tor -f ${torrc}"; ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID"; KillSignal = "SIGINT"; - TimeoutSec = 30; + TimeoutSec = cfg.settings.ShutdownWaitLength + 30; # Wait a bit longer than ShutdownWaitLength before actually timing out Restart = "on-failure"; LimitNOFILE = 32768; RuntimeDirectory = [