Rebased 2716 graceful restart

Supersedes #2716 which became nonmergeable due to age
Includes @scien's change (unsquashed because I don't know how to squash and retain credit)
Formatting fix
Unit test