Only create MongoClient when necessary

Fix issue when task.failed() throws an exception after task has died

Fix semaphore leaking issue