fix agent shutdown

stop thread earlier, and clear the queue so the pg refs go away