Dear MIDAS users,
Today I committed a fix to the develop branch for a problem that can make mhttpd (or any other MIDAS program) crash with:
ss_semaphore_wait_for: semop/semtimedop(...) returned -1, errno 90 (Identifier removed)
[mhttpd,ERROR] [odb.cxx:3354:db_lock_database,ERROR] cannot lock ODB semaphore, timeout 10000 ms, ss_semaphore_wait_for() status 408, aborting...
What happened
Each crash came right after running odbedit from a different shell. That shell pointed MIDAS_DIR at the same experiment directory, but MIDAS_EXPT_NAME was set to a different value (two shell startup files disagreed). With POSIX shared memory (the default on macOS, also used on Linux), MIDAS names its shared memory and its semaphores differently:
- The shared memory segment name contains the experiment name, e.g.
/Online_ODB_SHM. The POSIXv3/v4 variants also add the user id.
- The SysV semaphores and the
.ODB.SHM file depend only on the experiment directory.
So the second program got its own copy of the ODB, loaded from the .ODB.SHM file on disk, but shared the semaphore with the running mhttpd. In its private copy, mhttpd showed up only as a stale entry from the last flush to disk. The second program therefore removed mhttpd as "timed out" and, when it exited, saw itself as the last ODB client. It then deleted the semaphore, and mhttpd aborted on its next ODB lock. Any MIDAS program can trigger this, not just odbedit, and nothing warns you beforehand.
The fix
Each POSIX shared memory segment now has a small lock file next to its .SHM file, e.g. .ODB.SHM.LOCK and .SYSMSG.SHM.LOCK, which records the shared memory name currently in use:
- Every process attached to the segment holds a shared
flock() on the lock file.
- A process that finds the file unlocked (nobody attached) writes its own shared memory name into it, then joins.
- A process that finds others attached reads the name. If it differs from its own, it refuses to start, before creating anything:
ss_shm_open: Shared memory "ODB" in directory "/home/exp/online/" is in use as "/Online_ODB_SHM",
but this program would use "/Other_ODB_SHM". All programs using the same experiment directory
must use the same experiment name (check MIDAS_EXPT_NAME) and user. Sorry, bye!
Some properties:
- Several experiments on one machine still work. They live in separate directories, so they have separate lock files and never see each other.
- No stale state. The kernel releases the locks when a process exits or crashes, or the machine reboots. Running one directory with different experiment names one after the other still works; the next program simply overwrites the name.
- No false refusals. Programs with the same experiment name starting at the same moment are all accepted.
- Event buffers are covered too, because they use the same shared memory code.
- Unaffected setups: SysV and MMAP shared memory and Windows are unaffected, since their names already depend only on the directory. If
flock() is not supported (e.g. on some network file systems), the check is skipped and MIDAS behaves as before.
To benefit from the guard, rebuild and restart all your MIDAS programs. A program built with an older MIDAS takes no lock, so it is neither protected nor detected. After the restart you'll see the new .*.SHM.LOCK files in your experiment directory. They are harmless and can stay there.
If you have ever seen unexplained "Identifier removed" crashes or clients "timed out" for no obvious reason, check that all programs using your experiment directory see the same MIDAS_EXPT_NAME.
Best regards,
Stefan
|