Backport assertion fixes to 1.7 branch

Backports the fixes from #1120 to the 1.7 maintenance branch.

cc @atambo