Issue 5839 integrity check discard changes versus close

This pull request fixes #5839 and #5841