Free unused memory for threads and cancel any outstanding threads on interpreter exit to avoid possible segmentaiton fault.