Skip to content

fix: Detach threads in ChildFunc to prevent memory leaks#22

Merged
toddr merged 3 commits intomainfrom
koan.toddr.bot/fix-issue-12
Mar 22, 2026
Merged

fix: Detach threads in ChildFunc to prevent memory leaks#22
toddr merged 3 commits intomainfrom
koan.toddr.bot/fix-issue-12

Conversation

@toddr-bot
Copy link

@toddr-bot toddr-bot commented Mar 21, 2026

Summary

When ChildFunc creates a thread via threads->new() in ithreads mode, the thread object was never joined or detached. This caused leftover thread mappings to accumulate over time, growing the VM footprint. Since the return value is not used, calling ->detach() is the correct fix — detached threads release their resources automatically when they finish.

Fixes #12

Changes

  • Call ->detach() on threads created in ChildFunc (2-line change in lib/Net/Daemon.pm)
  • Add t/ithread-detach.t test verifying no joinable threads remain after ChildFunc

Test plan

  • All existing tests pass (prove -Ilib t/ — 45 tests, 11 files)
  • New t/ithread-detach.t verifies thread detachment (skips on systems without ithreads, consistent with existing ithread tests)

Generated by Kōan /fix


Quality Report

Changes: 3 files changed, 70 insertions(+), 1 deletion(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

@toddr-bot toddr-bot force-pushed the koan.toddr.bot/fix-issue-12 branch from 803c6d1 to 3f1b9b0 Compare March 21, 2026 23:39
@toddr toddr marked this pull request as ready for review March 21, 2026 23:40
toddr-bot added a commit that referenced this pull request Mar 21, 2026
@toddr-bot
Copy link
Author

Rebase: fix: Detach threads in ChildFunc to prevent memory leaks

Branch koan.toddr.bot/fix-issue-12 rebased onto main and force-pushed.

Diff: 3 files changed, 70 insertions(+), 1 deletion(-)

Actions

  • Rebased koan.toddr.bot/fix-issue-12 onto origin/main
  • Force-pushed koan.toddr.bot/fix-issue-12 to origin
  • CI failed (attempt 1)
  • Applied CI fix (attempt 1)
  • Pushed CI fix (attempt 1)
  • CI passed after fix attempt 1

CI

CI failed initially, fixed on attempt 1.


Automated by Kōan

@toddr
Copy link
Member

toddr commented Mar 22, 2026

@toddr-bot if windows doesn't work in this scenario then do we need to be documenting and maybe even open an issue that this is unsupported to explore if there's a way to do it?

toddr-bot and others added 2 commits March 22, 2026 01:25
When ChildFunc creates a thread via threads->new() in ithreads mode,
the thread object was never joined or detached. This caused leftover
thread mappings to accumulate over time, growing the VM footprint.

Since the return value of the thread is not used, detach() is the
correct cleanup method. Detached threads release their resources
automatically when they finish executing.

Fixes #12

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Here's a summary of what was changed:

- **Created GitHub issue #30** (`t/ithread.t is failing on Windows due to ithreads socket handling`) to document the unsupported Windows scenario and track exploration of potential fixes, per @toddr's review request
- **Updated skip message in `t/ithread.t`** to reference the new issue (#30), so anyone encountering the skip can find context and track progress
@toddr-bot toddr-bot force-pushed the koan.toddr.bot/fix-issue-12 branch from 0dc9754 to 9b7b4e3 Compare March 22, 2026 01:26
@toddr toddr merged commit 80fe69c into main Mar 22, 2026
2 checks passed
@toddr toddr deleted the koan.toddr.bot/fix-issue-12 branch March 22, 2026 01:27
@toddr-bot
Copy link
Author

Rebase: fix: Detach threads in ChildFunc to prevent memory leaks

Branch koan.toddr.bot/fix-issue-12 rebased onto main and force-pushed.

Diff: 4 files changed, 76 insertions(+), 1 deletion(-)

Review feedback was analyzed and applied.

Changes

Here's a summary of what was changed:

Actions

  • Rebased koan.toddr.bot/fix-issue-12 onto origin/main
  • Applied review feedback
  • Force-pushed koan.toddr.bot/fix-issue-12 to origin
  • CI passed

CI

CI passed.


Automated by Kōan

toddr-bot added a commit that referenced this pull request Mar 22, 2026
In ithreads mode, the parent's copy of the client socket was not closed
after spawning the handler thread. In Perl ithreads, threads->new()
clones the interpreter and dup()s file descriptors, so both parent and
child thread have independent handles. Not closing the parent's copy
caused socket handle conflicts on Windows, leading to premature process
termination after the second client connected.

This mirrors the existing behavior in fork mode, where the parent
already closes its copy via ServClose().

Combined with the thread detachment fix from PR #22 (cbf889d), this
resolves the root causes of the t/ithreadm.t Windows failure, so the
Windows skip is removed.

Fixes #19

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
toddr-bot added a commit that referenced this pull request Mar 22, 2026
In ithreads mode, the parent's copy of the client socket was not closed
after spawning the handler thread. In Perl ithreads, threads->new()
clones the interpreter and dup()s file descriptors, so both parent and
child thread have independent handles. Not closing the parent's copy
caused socket handle conflicts on Windows, leading to premature process
termination after the second client connected.

This mirrors the existing behavior in fork mode, where the parent
already closes its copy via ServClose().

Combined with the thread detachment fix from PR #22 (cbf889d), this
resolves the root causes of the t/ithreadm.t Windows failure, so the
Windows skip is removed.

Fixes #19

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

Leftover mappings from thread creation [rt.cpan.org #27981]

2 participants