Fix critical memory leaks and add graceful shutdown#26
Open
statico wants to merge 4 commits intodaniela-hase:mainfrom
Open
Fix critical memory leaks and add graceful shutdown#26statico wants to merge 4 commits intodaniela-hase:mainfrom
statico wants to merge 4 commits intodaniela-hase:mainfrom
Conversation
- Fix UDP socket leak: reuse discovery socket instead of creating new ones - Add graceful shutdown on SIGTERM/SIGINT for Docker compatibility - Cache snapshot.png in memory to prevent repeated file I/O - Prevent duplicate event listener registration in debug mode - Add error handlers for HTTP server and discovery socket - Implement shutdown() method to properly clean up resources Co-authored-by: Claude Code <claude@anthropic.com>
- Store and properly clean up TCP proxy instances on shutdown - Fix 'this' binding in HTTP server listen callback - Clean up SOAP client HTTP agent in config-builder - Restore Date.prototype.getUTCHours after modification to prevent global pollution - Add try-finally blocks to ensure cleanup happens even on errors Co-authored-by: Claude Code <claude@anthropic.com>
- Create reusable xml2js Parser instance instead of new parser on every discovery message - Cache XML parser options to avoid recreating array/object on each parse - Extract discovery response generation to separate method for better code organization - Replace deprecated url.parse with modern URL API - Add error handling for XML parse failures in discovery This fixes a critical memory leak where a new Parser instance was created for every ONVIF discovery probe, which could happen hundreds of times per hour. Co-authored-by: Claude Code <claude@anthropic.com>
- Builds and pushes Docker image to ghcr.io on every commit to main - Also builds (but doesn't push) on pull requests for testing - Supports multi-platform builds (amd64 and arm64) - Uses GitHub Actions cache for faster builds - Automatically tags images with branch name, commit SHA, and 'latest' The workflow uses GITHUB_TOKEN for authentication, which is automatically provided by GitHub Actions - no manual token setup required. Co-authored-by: Claude Code <claude@anthropic.com>
Author
|
(Human here: This tool was awesome but clearly running out of memory after a few hours. I asked Claude to fix all memory leaks and it seems to be stable now.) |
|
Those fixes helped me, I need more fix and vendor WSDL to run offline: https://github.com/thom-vend/onvif-server |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR fixes multiple critical memory leaks that cause unbounded memory growth in Docker deployments, eventually hitting memory limits and causing instability.
Note: This work was completed entirely using Claude Code, Anthropic's CLI tool for software development.
Critical Issues Fixed
1. UDP Socket Leak (CRITICAL)
2. Missing Graceful Shutdown (CRITICAL)
3. TCP Proxy Leak (CRITICAL)
.end()on shutdown4. xml2js Parser Leak (CRITICAL)
xml2js.parseString()creates a new Parser instance on every call, which happened on every discovery probe5. SOAP Client HTTP Agent Leak (HIGH)
6. Snapshot File I/O (HIGH)
snapshot.pngread from disk synchronously on every HTTP request7. Context Loss Bug (MEDIUM)
thiscontext.bind(this)when creating HTTP server8. Duplicate Event Listeners (MEDIUM)
enableDebugOutput()called repeatedly9. Global Prototype Pollution (LOW)
Date.prototype.getUTCHoursmodified globally and never restoredAdditional Improvements
url.parse()with modern URL APITesting Recommendations
Monitor Docker container memory under load:
docker stats --format "table {{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}"Memory usage should now stabilize instead of growing unbounded, especially under continuous ONVIF discovery traffic.
Bonus: GitHub Actions Workflow
Added automatic Docker image builds to GitHub Container Registry on every commit, with multi-platform support (amd64/arm64).
Impact
These fixes address the root causes of memory leaks that would eventually hit the 1.5G Docker memory limit, causing container instability. The server should now run indefinitely without memory growth.
Developed with: Claude Code by Anthropic