[core] Fix unproper DB session and tornado callbacks closing when finishing.