I wrote a shell.nix
file to build the development environment for one of my projects. I'm using a shellHook
to ensure a postgresql server is started when you drop into the nix-shell
.
The shellHook
is essentially:
export PGDATA=$PWD/nix/pgdata
pg_ctl start --silent --log $PWD/log/pg.log
Despite the fact that pg_ctl
starts a server in the background, if I type Ctrl-C in the shell, the server shuts down. If I set up the same scenario outside of nix-shell, this does not happen.
I'm new to strace, but it looks to me like the postgresql process is receiving SIGINT
when I type Ctrl-C in my terminal:
$ strace -p $postgres_pid
strace: Process 20546 attached
select(6, [3 4 5], NULL, NULL, {tv_sec=51, tv_usec=289149}) = ? ERESTARTNOHAND (To be restarted if no handler)
--- SIGINT {si_signo=SIGINT, si_code=SI_KERNEL} ---
rt_sigprocmask(SIG_SETMASK, ~[ILL TRAP ABRT BUS FPE SEGV CONT SYS RTMIN RT_1], NULL, 8) = 0
write(2, "LOG: received fast shutdown req"..., 37) = 37
kill(20550, SIGTERM) = 0
...
The postgresql process is attached to the same controlling terminal (pts/12
) as my nix-shell process (though this is also true when I run it outside of nix-shell):
$ ps -p ${postgres_pid},${nixshell_pid} -o pid,ppid,wchan,tty,cmd
PID PPID WCHAN TT CMD
14608 18292 core_s pts/12 bash --rcfile /tmp/nix-shell-14608-0/rc
16355 1 core_s pts/12 /nix/store/xxxxxx-postgresql-9.6.8/bin/postgres
What's a good next step in debugging this? Should I read up on process groups?
Update: Trying a tip from another question, I found that this fixes the problem:
set -m
pg_ctl start --silent --log $PWD/log/pg.log
The weird thing is, according to $-
, the m
option was already set. Running echo $-
produces imBH
both before and after the set -m
.
I noticed that in my interactive shells (whether nix-shell or not), $-
is imBHs
. The s
is not present in the shellHook
context, and I can't find an explanation of its meaning in the docs for Bash's set
builtin. This may not be related though...