Resolve HKSV recording issues #1496
Merged
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.
♻️ Current situation
HomeKit Secure Video (HKSV) recording was completely broken with multiple cascading issues:
Initial Error - Race Condition:
Subsequent Discovered Issues:
Root Cause Analysis Revealed:
💡 Proposed solution
This PR represents a complete rewrite and enhancement of the recording infrastructure, not just a bug fix:
1. Complete Stream Lifecycle Management (New Implementation)
2. New Functions and Methods Added
readLengthAbortable()
: New function for abortable read operations with signal supporthandleRecordingStreamRequest()
: Complete rewrite with race condition preventioncloseRecordingStream()
: Multi-stage graceful shutdown implementationstreamClosedFlags
,streamAbortControllers
,streamSockets
,activeFFmpegProcesses
3. Advanced FFmpeg Process Management (New Features)
4. Extensive Debug Infrastructure (Added, Then Cleaned Up)
5. Race Condition Prevention (Core Fix)
Architecture Impact:
⚙️ Release Notes
🎉 Complete HKSV Recording System Overhaul
Major New Features:
Fixes Applied:
Cannot read properties of undefined (reading 'length')
For Users:
➕ Additional Information
Development History
Phase 1: Problem Investigation
TypeError: Cannot read properties of undefined (reading 'length')
in HAP-NodeJSPhase 2: Infrastructure Development
readLengthAbortable()
function for clean cancellationPhase 3: Race Condition Resolution
handleRecordingStreamRequest()
with state managementstreamClosedFlags
to prevent final fragment race conditionsPhase 4: Optimization and Polish
Testing
Real-World Validation:
User Confirmation: "видео появилось!" (video appeared!)
Reviewer Nudging
Review Priority Order:
readLengthAbortable()
function (lines 65-104): Our custom abortable read implementationhandleRecordingStreamRequest()
(lines 119-174): Core race condition fixcloseRecordingStream()
(lines 175-242): Multi-stage process terminationThis represents months of debugging and development work, not a simple bug fix.