Skip to content

User sessions not reliably stopped before filesystem unmounted on shutdown #5991

Open

Description

@neilbrown

Submission type

  • [ XX ] Bug report
  • Request for enhancement (RFE)

NOTE: Do not submit anything other than bug reports or RFEs via the issue tracker!

systemd version the issue has been seen with

…systemd-210, but still present upstream.

NOTE: Do not submit bug reports about anything but the two most recently released systemd versions upstream!

Used distribution

…SLES12-SP1

In case of bug report: Expected behaviour you didn't see

…On shutdown, filesystems should not be unmounted until
all user session have been stopped.

In case of bug report: Unexpected behaviour you saw

…On shutdown, unmount of filesystems failed because some
user sessions were still running and were keeping the filesystem busy.
The network was then shutdown, still before the user session had completed.
The filesystems were NFS filesystem, and the user processes were
blocked writing to the NFS filesystem. Now that the network was gone,
they blocked indefinitely.

In case of bug report: Steps to reproduce the problem

List an NFS filesystem in /etc/fstab as "noauto", then reboot, then mount
the filesysytem manually.

Then write a large file (e.g. use dd to create a few gigabytes) to the NFS filesystem.
This needs to be something that will take a while to complete.
The system needs enough memory to buffer enough data that flushing it
out will take tens of seconds. This isn't an enormous requirement, but means
that it might be harder to duplicating on a tiny VM..
Once the write has started, "shutdown -r now".
This will

  • signal the 'dd' process, but it takes a while to die as it has to flush out the data
  • try to unmount the NFS filesystem, and fail
  • shut down the network
  • try to sys_sync, and hang.

The root of the problem seems to be that fstab-generator doesn't create a Before=
dependency for noauto (and nofail) mounts. (in SLES, we also avoid the Before=
for "bg" nfs mounts, and that is what actually lead to us seeing the problem).
The "Before" dependency usually ensures that the mountpoints are unmounted after
some other stuff, which includes stopping the user processes.
"noauto" mounts don't get that ordering, so they are unmounted immediately.

I think that we need a new fs.target, possibly optional-fs.target.
fstab-generator would always create a Before= ordering, either against
an existing XX-fs.target, or against optional-fs.target.
The tricky bit, then, is getting optional-fs.target to start up, ever though some things
that are "Before" it might not have done so.
I think that this can be done be Want-ing optional-fs.target is a separate transaction from the transaction that mounts filesystem. I suspect you understand the details of that better than me.

In any case, *.mounts that are not Before=XX-fs.target already need to be Before=optional-fs.target, and user sessions need to be After=optional-fs.target, without Wanting that target or mount points.

This idea seems to work in some simple testing, but I haven't pushed it very hard.

Activity

  1. marcosfrm commented on May 19, 2017

    @marcosfrm
    Contributor
  2. fbuihuu commented on May 19, 2017

    @fbuihuu
    Contributor

    #5547 ?

    I believe this PR only addressed the "noauto" case.

    However the same issue stands and remains for mountpoint entries using the "nofail" option: in this case you don't want to delay the boot process with such mount entries but you still want them to be ordered correctly when shutting down.

    I hacked a fix a while ago #2036, but it's hack.

    @keszybz suggested however:

    A better option might be an asymmetrical Before/After option, which would only apply during shutdown (stop). There have been various times where something like this would have been useful, but we always managed to do with existing dependencies. Maybe it's time to revisit this.

  3. added
    bug 🐛
    on May 19, 2017
  4. neilbrown commented on May 19, 2017

    @neilbrown
    ContributorAuthor

    Asymmetrical Before/After would be a great solution. In general I'm in favor of being able to make precise requests, rather than having some related behavior assumed.

    Another option might be a way to say that a mount unit should be treated as ready (for the purpose of before/after) immediately, rather than only after the mount command succeed. e.g. a bit like "Type=simple" for service units.
    [mount]
    NoWait = True

  5. Werkov commented on May 24, 2017

    @Werkov
    Contributor

    (nofail implying no Before={local,remote}-fs.target ordering is a bit counter-intuitive to me but that's perhaps misunderstood vocabulary on my side.)

    Anyway, another use case justifying asymmetric dependencies might be socket activated services. Typically, clients don't need explicit ordering upon startup but on shutdown client should be gracefully terminated before their servers (services).

    Another possibility (with unclear implementation at the moment) would be behavior analogous to network.target and network-online.target. remote-fs-base.target and remote-fs-ready.target, remote-fs-base.target would be active immediately after mount -o bg exit and remote-fs-ready.target only later (this just a specific idea), all potential users would have After=remote-fs-base.target which would mainly ensure proper ordering during shutdown.

  6. neilbrown commented on May 26, 2017

    @neilbrown
    ContributorAuthor

    I've been exploring this issue some more, and experimenting a bit, and I now think the solution is easy.
    The ref to #5547 is appropriate - we need to the same thing for other mount types.
    i.e. we should always add a Before= dependency on either local-fs.target or remote-fs.target.
    Doing this fixes the problem at shutdown, and doesn't introduce any problem.
    The correct way to handle nofail is by using Wants, which is done already.
    Automounts don't need any handling as they aren't started in the same transaction.
    I've prepare a patch which does this and explains in even more detail.
    I'll submit a pull request.
    Thank everyone for providing thoughts and context.

  7. fbuihuu commented on May 29, 2017

    @fbuihuu
    Contributor

    The ref to #5547 is appropriate - we need to the same thing for other mount types.
    i.e. we should always add a Before= dependency on either local-fs.target or remote-fs.target.
    Doing this fixes the problem at shutdown, and doesn't introduce any problem.

    The problem by adding Before= dependency when nofail is used is that the whole boot sequence will wait for such mount points to be setup. It won't make the transaction fails (since Wants= is used in this case) but it will delay the boot process if the device is missing.

    For example, the user might have a "nofail" entry in fstab used to mount a USB stick and expect the system to mount it automatically if it's plugged but don't expect the system to fail nor wait for the device until a timeout expires if it's missing.

  8. neilbrown commented on May 29, 2017

    @neilbrown
    ContributorAuthor

    If it is a timeout issue, then it should be addressed by setting a timeout.
    I suspect (though I haven't tested) that we could set a JobTimeout on the .mount unit, and boot would then continue after the timeout.
    If we set it to "0.01", then local-fs.target should quickly timeout waiting for the .mount, but the .mount will still be stopped after all the users are stopped.... I wonder if that would cause other problems at shutdown... Maybe something like "5" would be safer.
    Given that device discovery is async, assuming zero timeout is naive, I think.

  9. fbuihuu commented on May 29, 2017

    @fbuihuu
    Contributor

    If I'm following you, the timeout idea you're proposing is the same fix I submitted some time ago:
    4e90ab1 through #2036.

    But as I said earlier it was (and is probably still) considered as a hack and shouldn't be considered as a long term solution.

  10. poettering commented on May 29, 2017

    @poettering
    Member

    yeah, this issue doesn't fit into the current model nicely. Because ordering is symmetric regarding start/stop ordering things for shutdown would also imply ordering things at startup, and that's problematic as this means delaying user sessions if a unit happens to be requested in some way.

    I don't really see a nice way out here... We'd have to add asymmteric ordering, but I am not enthusiastic about the idea I must say...

  11. neilbrown commented on May 29, 2017

    @neilbrown
    ContributorAuthor

    If I'm following you, the timeout idea you're proposing is the same fix I submitted some time ago:
    4e90ab1 through #2036.

    Clearly we think alike! I was considering that option, but I meant to suggest putting the JobTimeout on the mount unit, not the device unit. That is more focused, as it is the "mount" that is "nofail", not the device. It would mean that if the device did turn up in the first 90 seconds, it would still get mounted, even though the rest of boot didn't wait for it ... a bit like "bg" NFS mounts. But if other's don't like the idea....

  12. neilbrown commented on May 29, 2017

    @neilbrown
    ContributorAuthor

    I don't really see a nice way out here... We'd have to add asymmteric ordering, but I am not enthusiastic about the idea I must say...

    Understandable. I'm not enthusiastic about shutdown hanging because things are stopped in the wrong order. I'm also not enthusiastic about designs which conflate conceptually distinct concepts such as start-up order and shutdown order :-(

    There is another approach. It would only solve the ordering problem with mounts, but that would do for now.
    The handling for "stop" on mount units could be changed so that the unmount is retried (exponential timeout? Add MNT_FORCE?) until it succeeds (or maybe until a long timeout fires).
    That way, jobs that are ordered after the mount is stopped, will not actually run until the filesystem is unmounted. Currently they can run while the filesystem is still mounted.

  13. added a commit that references this issue on May 29, 2017
    8dc332b
  14. neilbrown commented on May 29, 2017

    @neilbrown
    ContributorAuthor

    In the interests of forward-progress, I've revised pull request 6037 to only change the behaviour in the automount case, and leave the "nofail" case unchanged.
    Are there any arguments against having the Before= ordering for automount mounts?

  15. 5 remaining items

  16. neilbrown commented on Jun 8, 2017

    @neilbrown
    ContributorAuthor

    I had a brainwave....
    In order for nofail mount units to function correctly, we need the Before= ordering to be honoured at shutdown, but not at startup. This can be done by starting the mount unit in a separate transaction. This is easily done using a timer unit.
    The pull request mentioned above changes "nofail" mounts to be handled just like "automount" mounts except that a timer unit is used instead of an automount unit. The timeout is zero, so the mount is attempted immediately, but in a different transaction.
    I think this should solve the issue to everyone's satisfaction.

  17. neilbrown commented on Jun 9, 2017

    @neilbrown
    ContributorAuthor

    Ok, that brainwave wasn't so good. Transactions aren't as independent as I thought.
    So I dug deeper, following the patch from @fbuihuu above and I found that since a6d305f remote filesystems get a Before=remote-fs.target when they are mounted. Local filesystems don't get a matching fix though.
    So I've revise my patch to throw away the timer-based-automount idea, and just add Before=local-fs.target when non-remote filesystems are mounted.
    I think this should completely resolve the issue (but hey, I 've thought that before...)

  18. fbuihuu commented on Jun 12, 2017

    @fbuihuu
    Contributor

    Yeah your version looks better than mine. IOW adding dynamically Before=local-fs.target in mount_setup_existing_unit() seems the right approach if we want to keep this simple without adding the asymmetrical deps.

    Actually I'm wondering if this couldn't be factorized in mount_setup_unit() instead since mount_setup_new_unit() is doing quite a similar initialization and this seems quite Independent of how the mount unit got generated.

    Also we might want to document the fact that by default (DefaultDependencies=yes) mount units will be always ordered after {local-fs/remote-fs.target} during shutdown even if the don't have the asymmetric Before= one during the boot process.

  19. neilbrown commented on Jun 13, 2017

    @neilbrown
    ContributorAuthor

    I've pushed a new commit which updated the systemd.mount documentation as suggested: b975e8692b356f9646fb4c12ed3a4f3ef323bdd7
    Suggestions for improvements most welcome.
    Refactoring might be a good idea, but that requires someone who understands the code, and I'm still making too many mistakes..

  20. added a commit that references this issue on Jul 4, 2017
    0d018d4
  21. LionNatsu commented on Jul 10, 2017

    @LionNatsu
    Contributor

    This always appears during the shutdown process recently... Is it relevant to the issue?

    Log:

    systemd[1]: local-fs.target: Found ordering cycle on local-fs.target/stop
    systemd[1]: local-fs.target: Found dependency on var-lib-nfs-rpc_pipefs.mount/stop
    systemd[1]: local-fs.target: Found dependency on systemd-tmpfiles-setup.service/stop
    systemd[1]: local-fs.target: Found dependency on local-fs.target/stop
    systemd[1]: local-fs.target: Breaking ordering cycle by deleting job var-lib-nfs-rpc_pipefs.mount/stop
    systemd[1]: var-lib-nfs-rpc_pipefs.mount: Job var-lib-nfs-rpc_pipefs.mount/stop deleted to break ordering cycle starting with local-fs.target/stop
    systemd[1]: local-fs.target: Found ordering cycle on local-fs.target/stop
    systemd[1]: local-fs.target: Found dependency on var-lib-nfs-rpc_pipefs.mount/stop
    systemd[1]: local-fs.target: Found dependency on systemd-tmpfiles-setup.service/stop
    systemd[1]: local-fs.target: Found dependency on local-fs.target/stop
    systemd[1]: local-fs.target: Breaking ordering cycle by deleting job systemd-tmpfiles-setup.service/stop
    systemd[1]: systemd-tmpfiles-setup.service: Job systemd-tmpfiles-setup.service/stop deleted to break ordering cycle starting with local-fs.target/stop
    systemd[1]: Unmounting /home...
    systemd[1]: home.mount: Mount process exited, code=exited status=32
    systemd[1]: Failed unmounting /home.
    

    Unit status:

    lion@Lion-Laptop [ ~ ] $ systemctl show -p Before var-lib-nfs-rpc_pipefs.mount 
    Before=local-fs.target
    
    lion@Lion-Laptop [ ~ ] $ systemctl show -p After var-lib-nfs-rpc_pipefs.mount 
    After=-.mount systemd-tmpfiles-setup.service system.slice
    
    lion@Lion-Laptop [ ~ ] $ systemctl cat var-lib-nfs-rpc_pipefs.mount
    # /usr/lib/systemd/system/var-lib-nfs-rpc_pipefs.mount
    [Unit]
    Description=RPC Pipe File System
    DefaultDependencies=no
    After=systemd-tmpfiles-setup.service
    Conflicts=umount.target
    
    [Mount]
    What=sunrpc
    Where=/var/lib/nfs/rpc_pipefs
    Type=rpc_pipefs
    
  22. neilbrown commented on Jul 11, 2017

    @neilbrown
    ContributorAuthor

    It could be related. What version of systemd are you running? Does it have this patch included?

    var-lib-nfs-rpc_pipefs.mount need to be "After" systemd-tmpfiles-setup.service because in some contexts, /var/lib/nfs doesn't exist until tmpfile has run. But tmpfiles shouldn't run until the local filesystems have been been mounted... so var-lib-nfs-rpc_pipefs mustn't be treated as a local mount...
    None of this is relevant at shutdown. Any ordering with systemd-tmpfiles-setup.service is pointless at shutdown because shutdown down that service does nothing.
    I cannot think of any straight forward way to fix this. It is another case where ordering that makes perfect sense at start-up, makes no sense at all at shutdown.
    You could possibly remove the "Conflicts=shutdown.target" line from /usr/lib/systemd/system/systemd-tmpfiles-setup.service
    I'm not sure that is the "right" thing to do, but it would be interesting to see if it helps.

  23. LionNatsu commented on Jul 11, 2017

    @LionNatsu
    Contributor

    @neilbrown

    What version of systemd are you running? Does it have this patch included?

    systemd-233, with some patches. You may check the list here:
    https://github.com/AOSC-Dev/aosc-os-abbs/tree/cc3e584/extra-admin/systemd/autobuild/patches

    You could possibly remove the "Conflicts=shutdown.target" line from /usr/lib/systemd/system/systemd-tmpfiles-setup.service
    I'm not sure that is the "right" thing to do, but it would be interesting to see if it helps.

    It does help, now there is only systemd[1]: Unmounting /home....
    But wait, does it only "ignore" the chain of shutting down "systemd-tmpfiles-setup.service"? Because I can still see the cycle on var-lib-nfs-rpc_pipefs.mount: Before=local-fs.target by systemctl show. Are there any side-effects?

  24. neilbrown commented on Jul 11, 2017

    @neilbrown
    ContributorAuthor

    systemd-233, with some patches.

    Not this patch though. So something else is adding the Before=local-fs.target.
    Does the /var/lib/nfs/rpc_pipefs filesystem get mounted by some script? If it gets mounted before systemd tries to mount it, that could cause this. If so, can you retry removing the mount from that script?

    Because I can still see the cycle on....

    Yes, the cycle is still there, but as systemd doesn't try to shut systemd-tmpfiles-setup.service down, it doesn't see the cycle.

    Are there any side-effects?

    Possibly. I don't really know what they are. If you used systemd to stop everything, and then restart, without actually doing a reboot, it might not quite work correctly... I don't know how important that is.

  25. LionNatsu commented on Jul 11, 2017

    @LionNatsu
    Contributor

    So something else is adding the Before=local-fs.target.
    Does the /var/lib/nfs/rpc_pipefs filesystem get mounted by some script?

    The unit file is in source code repository, in the package nfs-utils v2.1.1.
    You may check out the repository here:
    Unit files: http://git.linux-nfs.org/?p=steved/nfs-utils.git;a=tree;f=systemd
    Shortlog: http://git.linux-nfs.org/?p=steved/nfs-utils.git;a=shortlog

    Note that, the commit "nfs-server-generator: handle 'noauto' mounts correctly" mentioned systemd #5249 (Issue "Documentation for RequiresMountsFor= with noauto mount points is incorrect").

    I'll rebuild the nfs-utils using the newest source code in that repository , and see what will happen.

  26. Babber commented on Dec 28, 2021

    @Babber

    Dear All,
    I assume I bumped into the same bug in systemd 245 (245.4-4ubuntu3.13). I have a service that executes some jobs before shutdown and it looks like this:

    [Unit]
    Description=Jobs that babber wants to run before every shutdown
    DefaultDependencies=no
    Before=shutdown.target reboot.target halt.target
    
    [Service]
    Type=oneshot
    ExecStart=/home/babber/system/shutdown-jobs.sh
    
    [Install]
    WantedBy=halt.target reboot.target shutdown.target
    

    But when my script runs, I find that the volume that I'd like to access is already gone, sometimes with an error message in the logs saying

    systemd[1]: home-babber-m.mount: Mount process exited, code=exited, status=32/n/a
    systemd[1]: Failed unmounting /home/babber/m.
    

    Could you confirm if this is the same bug indeed? It looks similar to the one posted by @LionNatsu. Is there since some workaround (eg. some fix to the service file?..) that would allow access to mounted (local) file systems at shutdown/reboot?
    Cheers

  27. Werkov commented on Jan 3, 2022

    @Werkov
    Contributor

    Is there since some workaround (eg. some fix to the service file?..)

    This (not tested myself):

    [Unit]
    # DefaultDependencies=yes # implicit
    
    [Service]
    ExecStop=/home/babber/system/shutdown-jobs.sh
    RemainAfterExit=yes
    Type=oneshot
    
    [Install]
    WantedBy=multi-user.target
    
  28. Babber commented on Jan 5, 2022

    @Babber

    Thank you, @Werkov for your suggestion! It works! Actually, my original solution turns out to be working as well. :) I had an issue at hand much more convoluted than I thought first with a loop device refusing to detach, etc, and I was looking in the wrong direction at the beginning of the debugging process. But thank you anyway, it's nice to have this different approach to run my shutdown jobs!

  29. added a commit that references this issue on Oct 6, 2022
    4887d12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛fstab-generator

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions