Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http server race #1953

Merged
merged 3 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/init_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ test_init_too_many_poller_threads(void)
}

void
test_init_repeated()
test_init_repeated(void)
{
nng_init_params p = { 0 };
NUTS_PASS(nng_init(NULL));
Expand All @@ -167,7 +167,7 @@ concurrent_init(void *arg)
}

void
test_init_concurrent()
test_init_concurrent(void)
{
nng_thread *threads[4];
nng_mtx *m = NULL;
Expand Down
26 changes: 9 additions & 17 deletions src/supplemental/http/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ typedef struct http_sconn {
nni_http_handler *handler; // set if we deferred to read body
nni_http_handler *release; // set if we dispatched handler
bool close;
bool closed;
bool finished;
nni_aio *cbaio;
nni_aio *rxaio;
nni_aio *txaio;
nni_aio *txdataio;
nni_reap_node reap;
nni_atomic_flag closed;
} http_sconn;

typedef struct http_error {
Expand Down Expand Up @@ -310,16 +310,15 @@ http_sc_reap(void *arg)
}

static void
http_sc_close_locked(http_sconn *sc)
http_sconn_close(http_sconn *sc)
{
nni_http_conn *conn;

if (sc->closed) {
if (nni_atomic_flag_test_and_set(&sc->closed)) {
return;
}
NNI_ASSERT(!sc->finished);

sc->closed = true;
nni_aio_close(sc->rxaio);
nni_aio_close(sc->txaio);
nni_aio_close(sc->txdataio);
Expand All @@ -331,17 +330,6 @@ http_sc_close_locked(http_sconn *sc)
nni_reap(&http_sc_reap_list, sc);
}

static void
http_sconn_close(http_sconn *sc)
{
nni_http_server *s;
s = sc->server;

nni_mtx_lock(&s->mtx);
http_sc_close_locked(sc);
nni_mtx_unlock(&s->mtx);
}

static void
http_sconn_txdatdone(void *arg)
{
Expand Down Expand Up @@ -987,13 +975,16 @@ nni_http_server_init(nni_http_server **serverp, const nng_url *url)

nni_mtx_lock(&http_servers_lk);
NNI_LIST_FOREACH (&http_servers, s) {
nni_mtx_lock(&s->mtx);
if ((!s->closed) && (url->u_port == s->port) &&
(strcmp(url->u_hostname, s->hostname) == 0)) {
*serverp = s;
s->refcnt++;
nni_mtx_unlock(&s->mtx);
nni_mtx_unlock(&http_servers_lk);
return (0);
}
nni_mtx_unlock(&s->mtx);
}

// We didn't find a server, try to make a new one.
Expand Down Expand Up @@ -1065,7 +1056,7 @@ http_server_stop(nni_http_server *s)
// Stopping the server is a hard stop -- it aborts any work
// being done by clients. (No graceful shutdown).
NNI_LIST_FOREACH (&s->conns, sc) {
http_sc_close_locked(sc);
http_sconn_close(sc);
}
}

Expand Down Expand Up @@ -1580,7 +1571,8 @@ http_handle_dir(nni_aio *aio)

rv = 0;
if (nni_file_is_dir(pn)) {
sprintf(dst, "%s%s", NNG_PLATFORM_DIR_SEP, "index.html");
snprintf(dst, pnsz - strlen(pn), "%s%s", NNG_PLATFORM_DIR_SEP,
"index.html");
if (!nni_file_is_file(pn)) {
pn[strlen(pn) - 1] = '\0'; // index.html -> index.htm
if (!nni_file_is_file(pn)) {
Expand Down
Loading