-5

I'm using a simple pthreads process-shared mutex in a shared memory segment to coordinate multiple server instances.

Code is straightforward:
On startup server attaches to shared memory segment if it exists, or creates it if it doesn't:
shm_open(), mmap(MAP_SHARED) etc.

This works very well while testing, but once deployed after a while i come across cases where server instances don't coordinate at all. I can replicate this by deleting the shared memory segment after a server has started: future servers instances will create/use a new segment but the existing one is stuck with the old segment nobody uses so in effect it's isolated from the rest ...

So my guess is the shared memory segment in /dev/shm is getting deleted somehow, and not by me. This is the only thing that makes sense... What is going on here ??

Running debian with linux 4.9.

2
  • Is possible that you're recreating shm again? Could you paste your code for shm reading and which flags have you used to attach other server instances to this shm? Apr 23, 2018 at 11:00
  • Sure, here's the code. Apr 23, 2018 at 11:24

1 Answer 1

6

Ah, looks like systemd is the culprit:

From dev-shm-files-disappearing:

After hours of searching and reading, I found the culprit. It's a setting for systemd. The /etc/systemd/logind.conf contains default configuration options, with each of them commented out. The RemoveIPC option is set to yes by default. That option tells systemd to clean up interprocess communication (IPC) for "user accounts" who aren't logged in. This does not affect "system accounts"

In my case, the files and directories were being created for a user account, not a system account.

There are two possible solutions:

  1. Create the files with/for a system user -- a user created with the system option (adduser -r or adduser --system)
  2. Edit /etc/systemd/logind.conf, uncomment the line RemoveIPC=yes, change it to RemoveIPC=no, save, and reboot the system

In my case, I went with option #2 because the user was already created.

See also this post on postgresql mailing list.

Not the answer you're looking for? Browse other questions tagged or ask your own question.