[PR #1] [MERGED] Find and fix a verifiable bug with tests #2

Closed
opened 2026-02-27 20:18:22 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/NodeDaemon/NodeDaemon/pull/1
Author: @ersinkoc
Created: 11/5/2025
Status: Merged
Merged: 11/5/2025
Merged by: @ersinkoc

Base: mainHead: claude/identify-and-fix-bug-011CUoybhg5RCPhRPNAPvgXy


📝 Commits (1)

  • 05607e5 fix: prevent duplicate instance creation on process restart

📊 Changes

2 files changed (+361 additions, -4 deletions)

View changed files

📝 src/core/ProcessOrchestrator.ts (+104 -4)
tests/unit/restart-instance-bug.test.js (+257 -0)

📄 Description

Fixes a critical bug in ProcessOrchestrator where restarting a crashed process would create duplicate instance objects instead of reusing the existing one.

Bug Description:
The restartInstance method was calling startClusterInstance or startSingleProcess, which create new ProcessInstance objects and push them to the instances array. This caused:

  • Unbounded memory growth as instances array accumulated dead records
  • Broken restart tracking (new instances always started with restarts=0)
  • Incorrect process state reporting (showing multiple instances)
  • maxRestarts limit never being reached, defeating crash-loop protection

Fix:
Refactored restart logic to spawn processes directly without creating new instances:

  • Added spawnClusterWorkerForInstance() helper method
  • Added spawnSingleProcessForInstance() helper method
  • Modified restartInstance() to reuse existing instance objects
  • Process spawning now updates the existing instance's PID and state

Impact:

  • Instances array now maintains correct length (one per actual process)
  • Restart counter properly increments on each restart
  • maxRestarts protection now works as intended
  • Memory leak in long-running daemons is eliminated

Testing:
Added comprehensive test suite in restart-instance-bug.test.js that verifies:

  • Instances array length stays at 1 after multiple restarts
  • Restart counter increments correctly
  • Instance ID remains consistent across restarts
  • Memory growth is prevented
  • maxRestarts limit is properly enforced

Location: src/core/ProcessOrchestrator.ts:333-466


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/NodeDaemon/NodeDaemon/pull/1 **Author:** [@ersinkoc](https://github.com/ersinkoc) **Created:** 11/5/2025 **Status:** ✅ Merged **Merged:** 11/5/2025 **Merged by:** [@ersinkoc](https://github.com/ersinkoc) **Base:** `main` ← **Head:** `claude/identify-and-fix-bug-011CUoybhg5RCPhRPNAPvgXy` --- ### 📝 Commits (1) - [`05607e5`](https://github.com/NodeDaemon/NodeDaemon/commit/05607e523065a6da2e04e8dce6b9f101a8464130) fix: prevent duplicate instance creation on process restart ### 📊 Changes **2 files changed** (+361 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `src/core/ProcessOrchestrator.ts` (+104 -4) ➕ `tests/unit/restart-instance-bug.test.js` (+257 -0) </details> ### 📄 Description Fixes a critical bug in ProcessOrchestrator where restarting a crashed process would create duplicate instance objects instead of reusing the existing one. **Bug Description:** The `restartInstance` method was calling `startClusterInstance` or `startSingleProcess`, which create new ProcessInstance objects and push them to the instances array. This caused: - Unbounded memory growth as instances array accumulated dead records - Broken restart tracking (new instances always started with restarts=0) - Incorrect process state reporting (showing multiple instances) - maxRestarts limit never being reached, defeating crash-loop protection **Fix:** Refactored restart logic to spawn processes directly without creating new instances: - Added `spawnClusterWorkerForInstance()` helper method - Added `spawnSingleProcessForInstance()` helper method - Modified `restartInstance()` to reuse existing instance objects - Process spawning now updates the existing instance's PID and state **Impact:** - Instances array now maintains correct length (one per actual process) - Restart counter properly increments on each restart - maxRestarts protection now works as intended - Memory leak in long-running daemons is eliminated **Testing:** Added comprehensive test suite in restart-instance-bug.test.js that verifies: - Instances array length stays at 1 after multiple restarts - Restart counter increments correctly - Instance ID remains consistent across restarts - Memory growth is prevented - maxRestarts limit is properly enforced Location: src/core/ProcessOrchestrator.ts:333-466 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-02-27 20:18:22 +03:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/NodeDaemon#2
No description provided.