Fix infinite loop in LegacyTimePeriod::FindNthWeekday().
Fix ThreadPool exception on shutdown.

Fixes #3891