Fixed DETACH DATABASE Query Error for ReplicatedMergeTree Tables

Fixed the error that prevented the DETACH DATABASE query from stopping background tasks for ReplicatedMergeTree tables.