py, vm: Fix recent bug where state is freed too early.