Skip to content

hts_newthread() leaves the pthread attributes object undestroyed when thread creation fails#807

Open
xroche wants to merge 9 commits into
masterfrom
fix/thread-attr-leak-772
Open

hts_newthread() leaves the pthread attributes object undestroyed when thread creation fails#807
xroche wants to merge 9 commits into
masterfrom
fix/thread-attr-leak-772

Conversation

@xroche

@xroche xroche commented Jul 27, 2026

Copy link
Copy Markdown
Owner

hts_newthread() folds pthread_attr_init(), pthread_attr_setstacksize() and pthread_create() into one short-circuit condition, and destroys the attributes object only on the branch where all three succeeded. If setstacksize or create fails (EAGAIN under thread or memory exhaustion being the realistic case), attr stays initialised for good. glibc allocates nothing for a default-initialised attr, so nothing actually leaks on Linux. This is a portability and hygiene fix rather than a live bug.

The init has to come out of the chain instead of just gaining a destroy call: the shared condition cannot tell which of the three failed, and destroying an object pthread_attr_init() never initialised is undefined.

The imbalance is invisible to a leak checker, so the test counts it directly. A small LD_PRELOAD shim refuses pthread_create() and records which attributes objects nobody destroyed afterwards: 1 undestroyed on master, 0 with the fix.

Closes #772

xroche and others added 9 commits July 27, 2026 13:48
… thread creation fails

hts_newthread() folds pthread_attr_init(), pthread_attr_setstacksize() and
pthread_create() into one short-circuit condition, and destroys the attributes
object only on the branch where all three succeeded. If setstacksize or create
fails (EAGAIN under thread or memory exhaustion being the realistic case), attr
stays initialised for good. glibc allocates nothing for a default-initialised
attr, so nothing actually leaks on Linux. This is a portability and hygiene fix
rather than a live bug.

The init has to come out of the chain instead of just gaining a destroy call:
the shared condition cannot tell which of the three failed, and destroying an
object pthread_attr_init() never initialised is undefined.

The imbalance is invisible to a leak checker, so the test counts it directly. A
small LD_PRELOAD shim refuses pthread_create() and records which attributes
objects nobody destroyed afterwards: 1 undestroyed on master, 0 with the fix.

Closes #772

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Xavier Roche <roche@httrack.com>
Condense the two-line note above pthread_attr_init(), fix the "so that is can
be independent" typo on the line the diff moves, and count a refused spawn as
tested only when it carried an attributes object, so a NULL-attr create from
elsewhere cannot satisfy the guard on its own. Note why the shim's fixed slot
count and unlocked counters are enough.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Xavier Roche <roche@httrack.com>
Resolve the tests/Makefile.am TESTS-tail conflict: keep both 111 and
113 entries in numeric order.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Xavier Roche <roche@httrack.com>
Resolve the tests/Makefile.am TESTS-tail conflict: keep 110, 111 and
113 in numeric order.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Xavier Roche <roche@httrack.com>
Fix the TESTS-tail union merge's missing continuation backslash.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Xavier Roche <roche@httrack.com>
Register 113_engine-threadattr-leak.test in tests-list.mk after the
#845 TESTS-list restructure moved it out of tests/Makefile.am, and
keep this branch's libthreadattrfail build rules (EXTRA_DIST,
TESTS_ENVIRONMENT, check_LTLIBRARIES, *_la_* rules) that a wholesale
take-master resolution had dropped.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Xavier Roche <roche@httrack.com>
@xroche
xroche force-pushed the fix/thread-attr-leak-772 branch from efd2d1c to 469952a Compare July 27, 2026 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hts_newthread() leaves the pthread attributes object undestroyed when thread creation fails

1 participant