-
-
Notifications
You must be signed in to change notification settings - Fork 261
libpcp_web: fix libuv teardown; pmfind: defer context release and shutdown order #2557
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
Closed
+172
−17
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,12 +11,23 @@ | |
| * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * for more details. | ||
| */ | ||
| #include <stdlib.h> | ||
| #include <uv.h> | ||
| #include "dict.h" | ||
| #include "pmapi.h" | ||
| #include "source.h" | ||
| #include "pmwebapi.h" | ||
|
|
||
| /* | ||
| * uv_handle_get_loop() is not in older libuv (e.g. Ubuntu 18.04 packages). All | ||
| * libuv 1.x keep the owning loop as handle->loop (UV_HANDLE_FIELDS). | ||
| */ | ||
| static inline uv_loop_t * | ||
| pmfind_handle_loop(const uv_handle_t *handle) | ||
| { | ||
| return handle->loop; | ||
| } | ||
|
|
||
| extern dictType sdsDictCallBacks; | ||
| extern dictType sdsOwnDictCallBacks; | ||
| extern dictType sdsKeyDictCallBacks; | ||
|
|
@@ -31,13 +42,21 @@ typedef struct { | |
| typedef struct { | ||
| int status; /* exit status */ | ||
| int count; /* url count */ | ||
| int defer_pending; /* deferred source_release timers */ | ||
| char **urls; /* callers */ | ||
| uv_mutex_t mutex; | ||
| dict *uniq; | ||
| dict *params; | ||
| dict *contexts; | ||
| } sources_t; | ||
|
|
||
| typedef struct source_defer { | ||
| uv_timer_t timer; | ||
| sources_t *sp; | ||
| context_t *cp; | ||
| sds ctx; | ||
| } source_defer_t; | ||
|
|
||
| static void | ||
| source_release(sources_t *sp, context_t *cp, sds ctx) | ||
| { | ||
|
|
@@ -48,16 +67,51 @@ source_release(sources_t *sp, context_t *cp, sds ctx) | |
| } | ||
|
|
||
| static void | ||
| sources_release(void *arg, const struct dictEntry *entry) | ||
| source_defer_close(uv_handle_t *handle) | ||
| { | ||
| sources_t *sp = (sources_t *)arg; | ||
| context_t *cp = (context_t *)dictGetVal(entry); | ||
| sds ctx = (sds)dictGetKey(entry); | ||
| source_defer_t *d = (source_defer_t *)handle->data; | ||
|
|
||
| if (pmDebugOptions.discovery) | ||
| fprintf(stderr, "releasing context %s\n", ctx); | ||
| free(d); | ||
| } | ||
|
|
||
| source_release(sp, cp, ctx); | ||
| /* | ||
| * pmWebGroupContext calls on_done, then webgroup_deref_context(cp). Calling | ||
| * pmWebGroupDestroy from on_done frees cp first — use-after-free. Defer destroy | ||
| * to the next event-loop tick so deref runs while cp is still valid. | ||
| */ | ||
| static void | ||
| source_defer_cb(uv_timer_t *handle) | ||
| { | ||
| source_defer_t *d = (source_defer_t *)handle->data; | ||
| uv_loop_t *loop = pmfind_handle_loop((uv_handle_t *)handle); | ||
|
|
||
| source_release(d->sp, d->cp, d->ctx); | ||
| if (--d->sp->defer_pending == 0) | ||
| uv_stop(loop); | ||
| uv_close((uv_handle_t *)handle, source_defer_close); | ||
| } | ||
|
|
||
| static int | ||
| source_release_deferred(uv_loop_t *loop, sources_t *sp, context_t *cp, sds ctx) | ||
| { | ||
| source_defer_t *d = malloc(sizeof(*d)); | ||
|
|
||
| if (d == NULL) { | ||
| fprintf(stderr, "%s: out of memory deferring context release\n", | ||
| pmGetProgname()); | ||
| return -ENOMEM; | ||
| } | ||
| d->sp = sp; | ||
| d->cp = cp; | ||
| d->ctx = ctx; | ||
| uv_timer_init(loop, &d->timer); | ||
| d->timer.data = d; | ||
| if (uv_timer_start(&d->timer, source_defer_cb, 0, 0) != 0) { | ||
| free(d); | ||
| return -EINVAL; | ||
| } | ||
| sp->defer_pending++; | ||
| return 0; | ||
| } | ||
|
|
||
| static void | ||
|
|
@@ -166,8 +220,14 @@ on_source_done(sds context, int status, sds message, void *arg) | |
| if (remove) { | ||
| if (pmDebugOptions.discovery) | ||
| fprintf(stderr, "remove context %s\n", context); | ||
| source_release(sp, cp, context); | ||
| /* | ||
| * Remove from the dict before pmWebGroupDestroy. The lookup key is the | ||
| * PMWEBAPI context id (libpcp_web frees it in pmWebGroupDestroy); calling | ||
| * dictDelete after that uses freed memory and corrupts the heap. | ||
| */ | ||
| dictDelete(sp->contexts, context); | ||
| if (source_release_deferred(uv_default_loop(), sp, cp, context) < 0) | ||
| sp->status = 1; | ||
| } | ||
|
|
||
| if (release) { | ||
|
|
@@ -176,7 +236,9 @@ on_source_done(sds context, int status, sds message, void *arg) | |
| dictEntry *entry; | ||
| dictInitIterator(&iter, sp->contexts); | ||
| while ((entry = dictNext(&iter)) != NULL) { | ||
| sources_release(sp, entry); | ||
| if (source_release_deferred(uv_default_loop(), sp, | ||
| (context_t *)dictGetVal(entry), (sds)dictGetKey(entry)) < 0) | ||
| sp->status = 1; | ||
| } | ||
| } else if (pmDebugOptions.discovery) { | ||
| fprintf(stderr, "not yet releasing (count=%d)\n", count); | ||
|
|
@@ -198,14 +260,17 @@ on_source_info(pmLogLevel level, sds message, void *arg) | |
| static void | ||
| sources_discovery_start(uv_timer_t *arg) | ||
| { | ||
| uv_loop_t *loop = pmfind_handle_loop((uv_handle_t *)arg); | ||
| uv_handle_t *handle = (uv_handle_t *)arg; | ||
| sources_t *sp = (sources_t *)handle->data; | ||
| dict *dp = dictCreate(&sdsOwnDictCallBacks); | ||
| sds name, value; | ||
| int i, fail = 0, total = sp->count; | ||
|
|
||
| if (dp == NULL) | ||
| if (dp == NULL) { | ||
| uv_stop(loop); | ||
| return; | ||
| } | ||
| name = sdsnew("hostspec"); | ||
|
|
||
| for (i = 0; i < total; i++) { | ||
|
|
@@ -231,6 +296,11 @@ sources_discovery_start(uv_timer_t *arg) | |
|
|
||
| dictRelease(dp); | ||
| pmWebTimerClose(); | ||
| /* | ||
| * Allow uv_run() in source_discovery() to return: libpcp_web leaves uv_async | ||
| * and timers referenced until pmWebGroupClose(), which runs after the loop. | ||
| */ | ||
| uv_stop(loop); | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -281,12 +351,32 @@ source_discovery(int count, char **urls) | |
| uv_timer_init(loop, &timing); | ||
| uv_timer_start(&timing, sources_discovery_start, 0, 0); | ||
| uv_run(loop, UV_RUN_DEFAULT); | ||
| /* | ||
| * on_source_done defers pmWebGroupDestroy. Without uv_stop, a second | ||
| * UV_RUN_DEFAULT would block forever (uv_async, GC timer, ...). The last | ||
| * deferred callback calls uv_stop so this run exits after work is drained. | ||
| */ | ||
| if (find.defer_pending > 0) | ||
| uv_run(loop, UV_RUN_DEFAULT); | ||
|
|
||
| /* | ||
| * Close libpcp_web loop handles before uv_loop_close(); drain each round of | ||
| * uv_close callbacks on this loop (Approach A — minimal shutdown). | ||
| */ | ||
| pmWebGroupClose(&settings.module); | ||
| uv_run(loop, UV_RUN_DEFAULT); | ||
|
|
||
| pmWebTimerLoopFinalize(); | ||
| uv_run(loop, UV_RUN_DEFAULT); | ||
|
|
||
| uv_close((uv_handle_t *)&timing, NULL); | ||
| uv_run(loop, UV_RUN_DEFAULT); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh wow, so many explicit calls to uv_run ... are they all really necessary? |
||
|
|
||
| uv_loop_close(loop); | ||
|
|
||
| /* | ||
| * Finished, release all resources acquired so far | ||
| */ | ||
| pmWebGroupClose(&settings.module); | ||
| uv_mutex_destroy(&find.mutex); | ||
| dictRelease(find.uniq); | ||
| dictRelease(find.params); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just in terms of naming, "pmWebTimerDestroy" might be more consistent with other parts of libpcp_web?