 +         * care of setting up the draining timer. */
 +        ngx_quic_write_handler(c->write);
 +
++        /* The QUIC connection might have already been freed inside the write
++         * handler, in which case we are done. */
++        if (c->destroyed) {
++            return NGX_OK;
++        }
++
 +        return NGX_AGAIN;
 +    }
 +

+    }
+
+    /* Version mismatch, do version negotiation. */
+    if (pkt_version != QUICHE_PROTOCOL_VERSION) {
+        ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
+                       "quic version negotiation");
+

+    if (c->quic) {
+        if (ngx_quic_shutdown(c) == NGX_AGAIN) {
+            c->quic->handler = ngx_quic_close_connection;
+            return;
+        }
+
+        if (c->destroyed) {
+            return;
+        }
+    }
