Skip to content

Conversation

@rlefko
Copy link
Contributor

@rlefko rlefko commented Sep 15, 2025

Summary

This PR fixes a memory leak in the debug implementation of pthread initialization functions when pthread_mutex_init() or pthread_cond_init() fails.

Problem

In the debug implementation (used when DEBUGLEVEL >= 1), the ZSTD_pthread_mutex_init() and ZSTD_pthread_cond_init() functions allocate memory using ZSTD_malloc() but fail to free it if the subsequent pthread initialization calls fail. This leads to memory leaks in error conditions.

The affected functions are:

  • ZSTD_pthread_mutex_init() at lib/common/threading.c:146
  • ZSTD_pthread_cond_init() at lib/common/threading.c:167

Solution

The fix adds proper error handling to free the allocated memory when pthread initialization fails:

  1. Store the return value of pthread_mutex_init/pthread_cond_init
  2. If initialization fails (non-zero return), free the allocated memory and set the pointer to NULL
  3. Return the error code

Impact

This fix is particularly important for:

  • Long-running applications where repeated initialization failures could accumulate memory leaks
  • Resource-constrained environments where pthread initialization might fail due to system limits
  • Ensuring proper resource cleanup in all error paths

Testing

  • All existing tests pass (make check completed successfully)
  • Pool tests specifically verified (make poolTests && ./poolTests passed)
  • The fix only affects error paths and maintains the same interface behavior

Changes

  • Modified lib/common/threading.c to add proper cleanup on pthread initialization failure

This is a defensive programming improvement that ensures no resources are leaked even in uncommon error scenarios.

When pthread_mutex_init() or pthread_cond_init() fails in the debug
implementation (DEBUGLEVEL >= 1), the previously allocated memory was
not freed, causing a memory leak.

This fix ensures that allocated memory is properly freed when pthread
initialization functions fail, preventing resource leaks in error
conditions.

The issue affects:
- ZSTD_pthread_mutex_init() at lib/common/threading.c:146
- ZSTD_pthread_cond_init() at lib/common/threading.c:167

This is particularly important for long-running applications or
scenarios with resource constraints where pthread initialization
might fail due to system limits.
@meta-cla
Copy link

meta-cla bot commented Sep 15, 2025

Hi @rlefko!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@Cyan4973 Cyan4973 self-assigned this Sep 15, 2025
@meta-cla
Copy link

meta-cla bot commented Sep 15, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla bot added the CLA Signed label Sep 15, 2025
Copy link
Contributor

@Cyan4973 Cyan4973 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pretty straightforward fix.
Thanks @rlefko !

@Cyan4973 Cyan4973 merged commit 085cc93 into facebook:dev Sep 20, 2025
104 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants