Fix application hang at shutdown