diff --git a/res/res_fax_spandsp.c b/res/res_fax_spandsp.c
index 2226d20c4c35f1ca5d5812de965c438d63d55af2..bc3b810adb2c9342556436ad2bb1c5c495c3008e 100644
--- a/res/res_fax_spandsp.c
+++ b/res/res_fax_spandsp.c
@@ -176,8 +176,25 @@ static void set_ecm(t30_state_t *t30_state, struct ast_fax_session_details *deta
 static void session_destroy(struct spandsp_pvt *p)
 {
 	struct ast_frame *f;
+	t30_state_t *t30_to_terminate;
 
-	t30_terminate(p->t30_state);
+	if (p->t30_state) {
+		t30_to_terminate = p->t30_state;
+	} else if (p->ist38) {
+#if SPANDSP_RELEASE_DATE >= 20080725
+		t30_to_terminate = &p->t38_state.t30;
+#else
+		t30_to_terminate = &p->t38_state.t30_state;
+#endif
+	} else {
+#if SPANDSP_RELEASE_DATE >= 20080725
+		t30_to_terminate = &p->fax_state.t30;
+#else
+		t30_to_terminate = &p->fax_state.t30_state;
+#endif
+	}
+
+	t30_terminate(t30_to_terminate);
 	p->isdone = 1;
 
 	ast_timer_close(p->timer);