Garbage collection freezes with defunct process

I’m trying to garbage collect my system and it gets stuck forever on:

 > sudo nix-collect-garbage -d
removing old generations of profile /nix/var/nix/profiles/default
removing old generations of profile /nix/var/nix/profiles/per-user/jakubgs/channels
removing old generations of profile /nix/var/nix/profiles/per-user/jakubgs/profile
removing old generations of profile /nix/var/nix/profiles/per-user/root/channels
removing old generations of profile /nix/var/nix/profiles/system
finding garbage collector roots...

And never exits. When I check the process status I see:

 > ps fa | grep nix-collect
18841 pts/0    S+     0:00  \_ sudo nix-collect-garbage -d
18842 pts/0    Zl+    0:00      \_ [nix-collect-gar] <defunct>

When I kill the defunct process with kill -9 4413 the process exits.
If I try to strace the defunct process I get:

strace: attach: ptrace(PTRACE_SEIZE, 13189): Operation not permitted

Not sure what I’m supposed to do.

One thing I find weird is the result of this command:

 > sudo nix-store --gc --print-dead | wc -l
finding garbage collector roots...
determining live/dead paths...
8151

That’s probably quite bad, right?

EDIT: I checked on my laptop and it also has a large number of these but garbage collections works.

It’s still broken. I have no idea what I’m supposed to do.

I straced a nix-store --delete call and I got this:

...
[pid  1099] lstat("/nix/store/trash/d2y3wrcwjrsf1sps036as6afx5jyg11c-awscli-1.18.128/lib/python3.8/site-packages/awscli", {st_mode=S_IFDIR|0755, st_size=10, ...}) = 0
[pid  1099] openat(AT_FDCWD, "/nix/store/trash/d2y3wrcwjrsf1sps036as6afx5jyg11c-awscli-1.18.128/lib/python3.8/site-packages/awscli", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 8
[pid  1099] fstat(8, {st_mode=S_IFDIR|0755, st_size=10, ...}) = 0
[pid  1099] getdents64(8, 0x114ad00 /* 10 entries */, 32768) = 304
[pid  1099] getdents64(8, 0x114ad00 /* 0 entries */, 32768) = 0
[pid  1099] close(8)                    = 0
[pid  1099] lstat("/nix/store/trash/d2y3wrcwjrsf1sps036as6afx5jyg11c-awscli-1.18.128/lib/python3.8/site-packages/awscli/examples", {st_mode=S_IFDIR|0755, st_size=82, ...}) = 0
[pid  1099] openat(AT_FDCWD, "/nix/store/trash/d2y3wrcwjrsf1sps036as6afx5jyg11c-awscli-1.18.128/lib/python3.8/site-packages/awscli/examples", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 8
[pid  1099] fstat(8, {st_mode=S_IFDIR|0755, st_size=82, ...}) = 0
[pid  1099] getdents64(8, 0x114ad00 /* 82 entries */, 32768) = 2656
[pid  1099] getdents64(8, 0x114ad00 /* 0 entries */, 32768) = 0
[pid  1099] close(8)                    = 0
[pid  1099] lstat("/nix/store/trash/d2y3wrcwjrsf1sps036as6afx5jyg11c-awscli-1.18.128/lib/python3.8/site-packages/awscli/examples/iam", {st_mode=S_IFDIR|0755, st_size=102, ...}) = 0
[pid  1099] openat(AT_FDCWD, "/nix/store/trash/d2y3wrcwjrsf1sps036as6afx5jyg11c-awscli-1.18.128/lib/python3.8/site-packages/awscli/examples/iam", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 8
[pid  1099] fstat(8, {st_mode=S_IFDIR|0755, st_size=102, ...}) = 0
[pid  1099] getdents64(8,  <unfinished ...>) = ?

It does not go beyond that point.