Skip to content

Commit cfaef07

Browse files
committed
Make output bucket brigade thread safe
Rewrote Alex Bligh's pull request so that it matches the coding style of this project and it should now compile with MSVC, which is not C99 compliant (i.e., doesn't support mixing of statements and declarations).
1 parent 952e5b6 commit cfaef07

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

mod_websocket.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,22 @@ static void mod_websocket_data_framing(const WebSocketServer *server,
472472
{
473473
WebSocketState *state = server->state;
474474
request_rec *r = state->r;
475-
apr_bucket_brigade *obb =
476-
apr_brigade_create(r->pool, r->connection->bucket_alloc);
475+
apr_pool_t *pool = NULL;
476+
apr_bucket_alloc_t *bucket_alloc;
477+
apr_bucket_brigade *obb;
477478

478-
if (obb != NULL) {
479+
/* We cannot use the same bucket allocator for the ouput bucket brigade
480+
* obb as the one associated with the connection (r->connection->bucket_alloc)
481+
* because the same bucket allocator cannot be used in two different
482+
* threads, and we use the connection bucket allocator in this
483+
* thread - see docs on apr_bucket_alloc_create(). This results in
484+
* occasional core dumps. So create our own bucket allocator and pool
485+
* for output thread bucket brigade. (Thanks to Alex Bligh -- abligh)
486+
*/
487+
488+
if ((apr_pool_create(&pool, r->pool) == APR_SUCCESS) &&
489+
((bucket_alloc = apr_bucket_alloc_create(pool)) != NULL) &&
490+
((obb = apr_brigade_create(pool, bucket_alloc)) != NULL)) {
479491
unsigned char block[BLOCK_DATA_SIZE];
480492
apr_int64_t block_size;
481493
apr_int64_t extension_bytes_remaining = 0;

mod_websocket_draft76.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ static int mod_websocket_method_handler(request_rec *r)
535535
/* If the plugin supplies an on_connect function, it must return non-null on success */
536536
if ((conf->plugin->on_connect == NULL) ||
537537
((plugin_private = conf->plugin->on_connect(&server)) != NULL)) {
538+
apr_pool_t *pool = NULL;
539+
apr_bucket_alloc_t *bucket_alloc;
538540
apr_bucket_brigade *obb;
539541

540542
/* Now that the connection has been established, disable the socket timeout */
@@ -548,9 +550,9 @@ static int mod_websocket_method_handler(request_rec *r)
548550
ap_send_interim_response(r, 1);
549551

550552
/* Create the output bucket brigade */
551-
obb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
552-
553-
if (obb != NULL) {
553+
if ((apr_pool_create(&pool, r->pool) == APR_SUCCESS) &&
554+
((bucket_alloc = apr_bucket_alloc_create(pool)) != NULL) &&
555+
((obb = apr_brigade_create(pool, bucket_alloc)) != NULL)) {
554556
unsigned char block[BLOCK_DATA_SIZE], *extended_data = NULL;
555557
apr_off_t extended_data_offset = 0;
556558
apr_size_t block_size, data_length = 0, extended_data_size = 0;

0 commit comments

Comments
 (0)