Fix memory leak bug#41
Merged
Merged
Conversation
There was a problem hiding this comment.
Code Review
This pull request removes the inline specifier from GetThreadWorkerNum in threadpool.cc to prevent potential linker errors. Reviewers suggested also removing the inline specifier from SetThreadWorkerNum to avoid similar ODR violations, marking the getter as const for better correctness, and addressing a potential data race on the thread_nums variable by using std::atomic.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Removes the inline specifier from the out-of-line definition of ThreadPool::GetThreadWorkerNum() in
thread_pool/src/threadpool.ccso the linker has an externally-visible definition to bind to from other translation units. Unblocks the weekly Valgrind CI build, which has been failing for at least three consecutive weeks (runs 25625309388 / 25987277977 / 26357808039 on 2026-05-10, 05-17, 05-24).Problem
weekly-valgrind.ymlfails at the Build step (before valgrind runs) with:Root cause
The function is declared in the public header but defined as inline in a .cc file:
thread_pool/include/threadpool.h:68 — int GetThreadWorkerNum(); (declaration only)thread_pool/src/threadpool.cc:43 — inline int ThreadPool::GetThreadWorkerNum() { return thread_nums; }This violates the C++ ODR for inline functions: an inline function must have its definition visible in every translation unit that calls it. With the body confined to
threadpool.cc:threadpool.ccitself can satisfy the inline call (its own internal callers at lines 79/88/90 inline fine).net_server.cc) see only the declaration and emit external symbol references.Why
ci.ymlmasks it: the default-O2build path produces a symbol layout that links anyway. The Valgrind workflow builds with-O1 -g -fno-omit-frame-pointer(required for accurate stacks), and at-O1GCC's emission policy for inline in-.cc functions diverges, leaving the external references unresolved.Test plan
make clean&&make -jN CXXFLAGS_EXTRA="-O1 -g -fno-omit-frame-pointer" CFLAGS_EXTRA="-O1 -g -fno-omit-frame-pointer" NGHTTP2_CFLAGS_EXTRA="-O1 -g-fno-omit-frame-pointer" — clean build, no link errors; produces test_runner + server_runner
./test_runner basic— 6/6 passRisk
Minimal. The function is a trivial one-line int accessor with no callers taking its address. Behavior is identical at every optimization level; only the symbol's linkage attribute changes.