diff --git a/pbx/pbx_dundi.c b/pbx/pbx_dundi.c
index e356d6fdeffcd84615b80bb45cecb8cebfeffbd4..a94d71cc42083f35fd5236788995ddca6ee65413 100644
--- a/pbx/pbx_dundi.c
+++ b/pbx/pbx_dundi.c
@@ -4982,13 +4982,14 @@ static int unload_module(void)
 
 	close(netsocket);
 	io_context_destroy(io);
-	ast_sched_context_destroy(sched);
 
 	mark_mappings();
 	prune_mappings();
 	mark_peers();
 	prune_peers();
 
+	ast_sched_context_destroy(sched);
+
 	return 0;
 }