Ensure render and layout tasks get destructed before main thread finishes

Fixes #1097.