Fix assertion failure on unclean shutdown

See #1509