Remove unsafePerformIO.
Replace cmpsort with Data.List.sortBy
Process can terminate gracefully now.