pty: Fix input race when closing