More http/request shutdown issues.

This commit is contained in:
Cory McWilliams 2024-03-17 12:38:37 -04:00
parent e5ba51b80a
commit af25782185

View File

@ -156,17 +156,30 @@ static void _http_connection_on_close(uv_handle_t* handle)
_http_connection_destroy(connection, "handle closed"); _http_connection_destroy(connection, "handle closed");
} }
static void _http_request_destroy(tf_http_request_t* request)
{
tf_http_close_callback* on_close = request->on_close;
if (on_close)
{
request->on_close = NULL;
tf_trace_begin(request->http->trace, request->connection && request->connection->trace_name ? request->connection->trace_name : "websocket");
on_close(request);
tf_trace_end(request->http->trace);
}
}
static void _http_connection_destroy(tf_http_connection_t* connection, const char* reason) static void _http_connection_destroy(tf_http_connection_t* connection, const char* reason)
{ {
connection->is_shutting_down = true; connection->is_shutting_down = true;
if (connection->request && connection->request->on_close) if (connection->request)
{ {
tf_http_close_callback* on_close = connection->request->on_close; _http_request_destroy(connection->request);
connection->request->on_close = NULL; if (connection->request->ref_count == 0)
tf_trace_begin(connection->http->trace, connection->trace_name ? connection->trace_name : "websocket"); {
on_close(connection->request); tf_free(connection->request);
tf_trace_end(connection->http->trace); }
connection->request = NULL;
} }
if (connection->tcp.data && !uv_is_closing((uv_handle_t*)&connection->tcp)) if (connection->tcp.data && !uv_is_closing((uv_handle_t*)&connection->tcp))
@ -949,29 +962,28 @@ void tf_http_request_ref(tf_http_request_t* request)
void tf_http_request_unref(tf_http_request_t* request) void tf_http_request_unref(tf_http_request_t* request)
{ {
bool connection_destroyed = false; tf_http_connection_t* connection = request->connection;
if (--request->connection->ref_count == 0)
{
if (request->connection->http->is_shutting_down)
{
_http_connection_destroy(request->connection, "unref during shutdown");
connection_destroyed = true;
}
else if (!request->connection->is_websocket)
{
_http_reset_connection(request->connection);
}
}
if (--request->ref_count == 0) if (--request->ref_count == 0)
{ {
if (!connection_destroyed) _http_request_destroy(request);
if (connection)
{ {
request->connection->request = NULL; connection->request = NULL;
} }
tf_free(request); tf_free(request);
} }
if (--connection->ref_count == 0)
{
if (connection->http->is_shutting_down)
{
_http_connection_destroy(connection, "unref during shutdown");
}
else if (!connection->is_websocket)
{
_http_reset_connection(connection);
}
}
} }
static const char* _http_connection_get_header(const tf_http_connection_t* connection, const char* name) static const char* _http_connection_get_header(const tf_http_connection_t* connection, const char* name)