[systemd-devel] Using *.path units without races?

Uwe Geuder systemd-devel-ugeuder at snkmail.com
Tue Mar 17 15:51:29 UTC 2020


Hi!

I have wondered for a while how I can use *.path units without (too bad)
races.

Since
https://github.com/systemd/systemd/pull/13509/commits/06582e42de65a61d0238a18720a12b6353edb7cd
the behaviour has been become much clearer, but I must admit I still
don't get it.

https://www.freedesktop.org/software/systemd/man/systemd.path.html
says

> When a service unit triggered by a path unit terminates (regardless
> whether it exited successfully or failed), monitored paths are checked
> immediately again, and the service accordingly restarted instantly

I'm am not sure I understand what that is supposed to mean.

So lets take a simple example:

  DirectoryNotEmpty=/tmp/sd-test/

When I call a

  touch /tmp/sd-test/1.foo

my service gets activated, that's all fine. In order to avoid an endless loop
my service moves the files in the watched directory out of the way. (The man
page mentions that systemd would limit the loop anyway if I didn't do it.)

But now assume that during the service is running (easily tested with a sleep
in the service) I call a

  touch /tmp/sd-test/2.foo

and later (after my sleep) the "service unit triggered by" my "path unit
terminates".

According to the man page "monitored paths are checked immediately again
and the service accordingly restarted instantly". However, I have not
been able to produce any such "instant restart".

It appears to me that the path has to be back in state "waiting" already,
before any newly created file will be noticed.

I also tested

  PathExistsGlob=/tmp/sd-test/*
  PathModified=/tmp/sd-test/

but the behaviour seems to be the same in all cases. When the service
terminates the path goes to waiting and only *after* that new events will be
noticed. No "instant restart" for anything that happend while path was
running.

What am I missing here? Are *.path units at all supposed to handle several
files appearing at random times in a reasonable racefree manner? What would be
the correct pattern to do so?

Of course I can implement my service to recheck before it exits. But that will
never be racefree, and it will fail completely should the service fail because
of an error. The man page suggests to me that systemd would do more for me.

Regards,

Uwe

Uwe Geuder
Neuro Event Labs Oy
Tampere, Finland
uwe.gxuder at neuroeventlabs.com (bot check: fix 1 obvious typo)


More information about the systemd-devel mailing list