Index: /pjproject/trunk/pjsip/include/pjsip/sip_transaction.h
===================================================================
--- /pjproject/trunk/pjsip/include/pjsip/sip_transaction.h	(revision 3070)
+++ /pjproject/trunk/pjsip/include/pjsip/sip_transaction.h	(revision 3071)
@@ -98,5 +98,4 @@
     pj_uint32_t			hashed_key;	/**< Key's hashed value.    */
     pj_str_t			branch;         /**< The branch Id.         */
-    pjsip_tpselector		tp_sel;		/**< Transport selector.    */
 
     /*
@@ -122,4 +121,8 @@
     unsigned			transport_flag;	/**< Miscelaneous flag.	    */
     pj_status_t			transport_err;	/**< Internal error code.   */
+    pjsip_tpselector		tp_sel;		/**< Transport selector.    */
+    pjsip_tx_data	       *pending_tx;	/**< Tdata which caused
+						     pending transport flag
+						     to be set on tsx.	    */
 
     /*
Index: /pjproject/trunk/pjsip/include/pjsip/sip_transport.h
===================================================================
--- /pjproject/trunk/pjsip/include/pjsip/sip_transport.h	(revision 3070)
+++ /pjproject/trunk/pjsip/include/pjsip/sip_transport.h	(revision 3071)
@@ -570,4 +570,8 @@
     pjsip_tpselector	    tp_sel;
 
+    /**
+     * Arbitrary data attached by PJSIP modules.
+     */
+    void		    *mod_data[PJSIP_MAX_MODULE];
 };
 
Index: /pjproject/trunk/pjsip/src/pjsip/sip_transaction.c
===================================================================
--- /pjproject/trunk/pjsip/src/pjsip/sip_transaction.c	(revision 3070)
+++ /pjproject/trunk/pjsip/src/pjsip/sip_transaction.c	(revision 3071)
@@ -582,4 +582,13 @@
 static void mod_tsx_layer_unregister_tsx( pjsip_transaction *tsx)
 {
+    if (mod_tsx_layer.mod.id == -1) {
+	/* The transaction layer has been unregistered. This could happen
+	 * if the transaction was pending on transport and the application
+	 * is shutdown. See http://trac.pjsip.org/repos/ticket/1033. In
+	 * this case just do nothing.
+	 */
+	return;
+    }
+
     pj_assert(tsx->transaction_key.slen != 0);
     //pj_assert(tsx->state != PJSIP_TSX_STATE_NULL);
@@ -1116,18 +1125,25 @@
 	pj_time_val timeout = {0, 0};
 
-	/* Reschedule timeout timer to destroy this transaction. */
+	/* If we're still waiting for a message to be sent.. */
 	if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
-	    tsx->transport_flag |= TSX_HAS_PENDING_DESTROY;
-	} else {
-	    /* Cancel timeout timer. */
-	    if (tsx->timeout_timer.id != 0) {
-		pjsip_endpt_cancel_timer(tsx->endpt, &tsx->timeout_timer);
-		tsx->timeout_timer.id = 0;
-	    }
-
-	    tsx->timeout_timer.id = TIMER_ACTIVE;
-	    pjsip_endpt_schedule_timer( tsx->endpt, &tsx->timeout_timer, 
-					&timeout);
-	}
+	    /* Disassociate ourselves from the outstanding transmit data
+	     * so that when the send callback is called we will be able
+	     * to ignore that (otherwise we'll get assertion, see
+	     * http://trac.pjsip.org/repos/ticket/1033)
+	     */
+	    tsx->pending_tx->mod_data[mod_tsx_layer.mod.id] = NULL;
+	    tsx->pending_tx = NULL;
+	    tsx->transport_flag &= ~(TSX_HAS_PENDING_TRANSPORT);
+	}
+
+	/* Cancel timeout timer. */
+	if (tsx->timeout_timer.id != 0) {
+	    pjsip_endpt_cancel_timer(tsx->endpt, &tsx->timeout_timer);
+	    tsx->timeout_timer.id = 0;
+	}
+
+	tsx->timeout_timer.id = TIMER_ACTIVE;
+	pjsip_endpt_schedule_timer( tsx->endpt, &tsx->timeout_timer,
+				    &timeout);
 
 
@@ -1637,4 +1653,15 @@
     pjsip_tx_data *tdata = send_state->tdata;
     struct tsx_lock_data lck;
+
+    /* Check if transaction has cancelled itself from this transmit
+     * notification (https://trac.pjsip.org/repos/ticket/1033).
+     */
+    if (tdata->mod_data[mod_tsx_layer.mod.id] == NULL) {
+	return;
+    }
+
+    /* Reset */
+    tdata->mod_data[mod_tsx_layer.mod.id] = NULL;
+    tsx->pending_tx = NULL;
 
     lock_tsx(tsx, &lck);
@@ -1907,4 +1934,11 @@
     pjsip_tx_data_add_ref(tdata);
 
+    /* Also attach ourselves to the transmit data so that we'll be able
+     * to unregister ourselves from the send notification of this
+     * transmit data.
+     */
+    tdata->mod_data[mod_tsx_layer.mod.id] = tsx;
+    tsx->pending_tx = tdata;
+
     /* Begin resolving destination etc to send the message. */
     if (tdata->msg->type == PJSIP_REQUEST_MSG) {
@@ -1915,6 +1949,9 @@
 	if (status == PJ_EPENDING)
 	    status = PJ_SUCCESS;
-	if (status != PJ_SUCCESS)
+	if (status != PJ_SUCCESS) {
 	    pjsip_tx_data_dec_ref(tdata);
+	    tdata->mod_data[mod_tsx_layer.mod.id] = NULL;
+	    tsx->pending_tx = NULL;
+	}
 	
 	/* Check if transaction is terminated. */
@@ -1930,6 +1967,9 @@
 	if (status == PJ_EPENDING)
 	    status = PJ_SUCCESS;
-	if (status != PJ_SUCCESS)
+	if (status != PJ_SUCCESS) {
 	    pjsip_tx_data_dec_ref(tdata);
+	    tdata->mod_data[mod_tsx_layer.mod.id] = NULL;
+	    tsx->pending_tx = NULL;
+	}
 
 	/* Check if transaction is terminated. */
