[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[libmicrohttpd] 26/156: Fixed very rare data races when closing upgraded
From: |
gnunet |
Subject: |
[libmicrohttpd] 26/156: Fixed very rare data races when closing upgraded connection |
Date: |
Sun, 28 May 2023 17:51:19 +0200 |
This is an automated email from the git hooks/post-receive script.
karlson2k pushed a commit to tag v0.9.77
in repository libmicrohttpd.
commit a59760c5db93bdce01810afc7639e1c82c3198b3
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
AuthorDate: Mon Jun 13 12:34:16 2022 +0300
Fixed very rare data races when closing upgraded connection
---
src/microhttpd/daemon.c | 38 ++++++++++++++++++++++++++++++++++++++
src/microhttpd/internal.h | 15 +++++++++++++++
src/microhttpd/response.c | 7 ++++---
3 files changed, 57 insertions(+), 3 deletions(-)
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
index e5ecc9ef..0b9cc258 100644
--- a/src/microhttpd/daemon.c
+++ b/src/microhttpd/daemon.c
@@ -3269,6 +3269,44 @@ MHD_resume_connection (struct MHD_Connection *connection)
}
+#ifdef UPGRADE_SUPPORT
+/**
+ * Mark upgraded connection as closed by application.
+ *
+ * The @a connection pointer must not be used after call of this function
+ * as it may be freed in other thread immediately.
+ * @param connection the upgraded connection to mark as closed by application
+ */
+void
+MHD_upgraded_connection_mark_app_closed_ (struct MHD_Connection *connection)
+{
+ /* Cache 'daemon' here to avoid data races */
+ struct MHD_Daemon *const daemon = connection->daemon;
+#if defined(MHD_USE_THREADS)
+ mhd_assert (NULL == daemon->worker_pool);
+#endif /* MHD_USE_THREADS */
+ mhd_assert (NULL != connection->urh);
+ mhd_assert (0 != (daemon->options & MHD_TEST_ALLOW_SUSPEND_RESUME));
+
+ MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
+ connection->urh->was_closed = true;
+ connection->resuming = true;
+ daemon->resuming = true;
+ MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
+ if ( (MHD_ITC_IS_VALID_ (daemon->itc)) &&
+ (! MHD_itc_activate_ (daemon->itc, "r")) )
+ {
+#ifdef HAVE_MESSAGES
+ MHD_DLOG (daemon,
+ _ ("Failed to signal resume via " \
+ "inter-thread communication channel.\n"));
+#endif
+ }
+}
+
+
+#endif /* UPGRADE_SUPPORT */
+
/**
* Run through the suspended connections and move any that are no
* longer suspended back to the active state.
diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h
index 04782656..f5c965ec 100644
--- a/src/microhttpd/internal.h
+++ b/src/microhttpd/internal.h
@@ -2408,4 +2408,19 @@ MHD_check_response_header_token_ci (const struct
MHD_Response *response,
void
internal_suspend_connection_ (struct MHD_Connection *connection);
+
+#ifdef UPGRADE_SUPPORT
+/**
+ * Mark upgraded connection as closed by application.
+ *
+ * The @a connection pointer must not be used after call of this function
+ * as it may be freed in other thread immediately.
+ * @param connection the upgraded connection to mark as closed by application
+ */
+void
+MHD_upgraded_connection_mark_app_closed_ (struct MHD_Connection *connection);
+
+#endif /* UPGRADE_SUPPORT */
+
+
#endif
diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c
index dd860974..f51fa686 100644
--- a/src/microhttpd/response.c
+++ b/src/microhttpd/response.c
@@ -1587,11 +1587,12 @@ MHD_upgrade_action (struct MHD_UpgradeResponseHandle
*urh,
}
#endif /* HTTPS_SUPPORT */
mhd_assert (MHD_CONNECTION_UPGRADE == connection->state);
- urh->was_closed = true;
- /* As soon as connection will be marked with BOTH
+ /* The next function will mark the connection as closed by application
+ * by setting 'urh->was_closed'.
+ * As soon as connection will be marked with BOTH
* 'urh->was_closed' AND 'urh->clean_ready', it will
* be moved to cleanup list by MHD_resume_connection(). */
- MHD_resume_connection (connection);
+ MHD_upgraded_connection_mark_app_closed_ (connection);
return MHD_YES;
case MHD_UPGRADE_ACTION_CORK_ON:
/* Unportable API. TODO: replace with portable action. */
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
- [libmicrohttpd] 19/156: thread-per-connection: do not cache connection timeout, (continued)
- [libmicrohttpd] 19/156: thread-per-connection: do not cache connection timeout, gnunet, 2023/05/28
- [libmicrohttpd] 25/156: Fixed 'int' instead of 'MHD_Result', gnunet, 2023/05/28
- [libmicrohttpd] 24/156: daemon.c: fixed possible uninitialized value, gnunet, 2023/05/28
- [libmicrohttpd] 28/156: websocket_threaded_example: minor fixes, gnunet, 2023/05/28
- [libmicrohttpd] 37/156: testcurl: fixed checking response headers as null-terminated string, gnunet, 2023/05/28
- [libmicrohttpd] 34/156: daemon: fixed delayed notification callback in thread-per-connection mode, gnunet, 2023/05/28
- [libmicrohttpd] 38/156: test_upgrade{,_large}: fixed HTTP/1.1 compatibility, gnunet, 2023/05/28
- [libmicrohttpd] 42/156: configure: try to detect whether eventfd is enabled, gnunet, 2023/05/28
- [libmicrohttpd] 35/156: mhd_assert: use "DEBUG" macro defined by some toolchains, gnunet, 2023/05/28
- [libmicrohttpd] 33/156: mhd_str: fixed compiler warning for compact code, gnunet, 2023/05/28
- [libmicrohttpd] 26/156: Fixed very rare data races when closing upgraded connection,
gnunet <=
- [libmicrohttpd] 30/156: configure: fixed underquoting, typos, wording, gnunet, 2023/05/28
- [libmicrohttpd] 32/156: configure: fixed typos in messages, gnunet, 2023/05/28
- [libmicrohttpd] 23/156: Fixed wrong variable type used for result of gnutls_priority_init(), gnunet, 2023/05/28
- [libmicrohttpd] 41/156: configure: used better detection of some functions when cross-compiling, gnunet, 2023/05/28
- [libmicrohttpd] 27/156: test_http_reasons: fixed compiler warnings, gnunet, 2023/05/28
- [libmicrohttpd] 29/156: Remove double adding of MHD_HTTP_HEADER_CONNECTION response header, gnunet, 2023/05/28
- [libmicrohttpd] 31/156: connection: skip no-op calling of body generation functions when response body is not used, gnunet, 2023/05/28
- [libmicrohttpd] 43/156: -typo, gnunet, 2023/05/28
- [libmicrohttpd] 21/156: internal.h: fixed incorrect use of enum value as preprocessor value, gnunet, 2023/05/28
- [libmicrohttpd] 40/156: Added new M4 helper macro, gnunet, 2023/05/28