Issue #9501: Improved shutdown handling to deal with module attributes correctly.
Issue #9501: Merged fix from 3.2.