crypto: implement CTHR_THREAD_CLOSE to avoid leaking memory
What changed, and why it matters
This commit fixes a thread resource leak in Monero's cryptography code. Previously, a helper macro called CTHR_THREAD_CLOSE was defined as doing nothing, meaning threads were created and joined but never properly detached. On systems using POSIX threads (pthreads), this can leave thread resources unreleased, causing gradual memory use growth. The fix makes CTHR_THREAD_CLOSE call pthread_detach, which tells the system it can clean up the thread after it finishes. This is a reliability and resource-management fix rather than a direct remote exploit, but resource exhaustion can sometimes affect service availability.
Apply the patch. Review whether CTHR_THREAD_CLOSE is invoked consistently after every CTHR_THREAD_JOIN in the codebase, and consider whether pthread_detach after join is the intended lifecycle or if the thread should instead be created detached. Monitor for any regressions in thread lifecycle behavior.
Security signals we found
Resource leak (thread handle / stack memory)
Potential memory growth / denial-of-service via resource exhaustion
Missing cleanup after thread join
Evidence from the diff
In src/crypto/c_threads.h, the POSIX implementation of CTHR_THREAD_CLOSE was an empty macro. Threads created via pthread_create are not automatically detached; without pthread_detach or a pthread_attr_t with detachstate set, the thread’s underlying resources (stack, thread descriptor) may persist until the process exits, even after pthread_join returns. The patch changes the macro to pthread_detach(thr), allowing the OS to reclaim the thread’s resources once it exits. This is a partial fix because it only addresses the POSIX path; the Windows path is unchanged and already had a CloseHandle implementation.
Changed components
src/crypto/c_threads.hPOSIX pthread-based thread wrapper macrosInspect captured patch +1 / −1
diff --git a/src/crypto/c_threads.h b/src/crypto/c_threads.h
index 033da25..267b91f 100644
--- a/src/crypto/c_threads.h
+++ b/src/crypto/c_threads.h
@@ -65,6 +65,6 @@
#define CTHR_THREAD_RETURN return NULL
#define CTHR_THREAD_CREATE(thr, func, arg) (pthread_create(&thr, NULL, func, arg) == 0)
#define CTHR_THREAD_JOIN(thr) pthread_join(thr, NULL)
-#define CTHR_THREAD_CLOSE(thr)
+#define CTHR_THREAD_CLOSE(thr) pthread_detach(thr)
#endif
Why this scored 35/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.