Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 13, 2025

🚀 Fix for Issue #6

This PR fixes the AccessViolationException that occurs when shutting down the application with Ctrl+C.

🔍 Problem Analysis

The issue was caused by improper disposal of the SplitMemoryLinks<ulong> instance during application shutdown. When the user pressed Ctrl+C, the application would terminate abruptly without properly disposing of memory-mapped resources, leading to:

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. 
This is often an indication that other memory is corrupt.

✅ Solution

Implemented comprehensive resource cleanup through:

  1. Resource Tracking: Modified Data.cs to track the disposable SplitMemoryLinks instance
  2. Graceful Disposal: Added DisposeLinks() method with exception handling to prevent disposal errors from causing crashes
  3. Application Lifecycle Management: Created LinksLifetimeService hosted service to handle application stopping events
  4. Multiple Cleanup Points: Added cleanup in both application lifetime events and program finally block

📂 Files Modified

  • Data.cs: Added static tracking of disposable links and safe disposal method
  • Program.cs: Added links disposal in finally block for additional safety
  • LinksLifetimeService.cs: New hosted service for handling application lifecycle events
  • Startup.cs: Registered the lifetime service
  • StartupWithRouting.cs: Registered the lifetime service

🧪 Testing

  • ✅ Solution compiles successfully for both net5 and net6 targets
  • ✅ All existing tests (8 tests) pass
  • ✅ No breaking changes to existing functionality

🔧 Technical Details

The solution uses ASP.NET Core's IHostApplicationLifetime to properly handle shutdown signals (SIGTERM/SIGINT) and ensures that:

  1. Resources are disposed before the application terminates
  2. Any exceptions during disposal are caught and logged (not propagated)
  3. Memory corruption during shutdown is prevented

This approach is robust and follows .NET best practices for resource management in web applications.


🤖 Generated with Claude Code


Resolves #6

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #6
@konard konard self-assigned this Sep 13, 2025
@konard konard changed the title [WIP] Bug Report 23.08.2021 Fix AccessViolationException on application shutdown (Ctrl+C) Sep 13, 2025
- Add resource tracking and safe disposal to Data.cs
- Create LinksLifetimeService for application lifecycle management
- Register cleanup handlers in both Startup classes
- Add disposal safety in Program.cs finally block
- Remove broken .editorconfig symlink

This prevents memory corruption during shutdown by ensuring
SplitMemoryLinks resources are properly disposed when the
application receives termination signals.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard marked this pull request as ready for review September 13, 2025 05:38
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.

Bug Report 23.08.2021

2 participants