Fix: Add comprehensive error handling and logging for WASM stack allocation on macOS #48
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.
Problem
Users on macOS 15 with Python 3.9 are experiencing crashes when calling
add_to_stack(store, -16)to allocate stack space for WASM operations (issue #46). Even with the defensive pattern from PR #47 that handles different return value types, users report Python still crashes at this line on macOS while working fine on Linux.From issue #46 comment:
The maintainer confirmed this appears to be a macOS-specific issue, suggesting the problem goes deeper than return value handling—potentially a segfault or WASM runtime incompatibility on macOS.
Solution
This PR adds comprehensive error handling and detailed debug logging throughout the WASM workflow to transform silent crashes into informative error messages with diagnostic information. While this may not fix the underlying macOS system-level issue, it provides the tools needed to diagnose and understand what's happening.
Changes
1. Enhanced WASM Initialization (lines 533-564)
Added try-except blocks around all initialization steps with detailed logging:
2. Protected Helper Functions (lines 566-593)
Added error handling to all memory and string operations:
write_memory()- with offset/size context in errorsread_memory()- with offset/size context in errorsencode_string()- with length context in errors3. Critical Stack Operation Protection (lines 595-679)
Wrapped the problematic
add_to_stack(store, -16)call with comprehensive error handling:4. Protected Cleanup Operations
All stack pointer restoration calls are now wrapped in try-except to prevent cleanup failures from masking the original error:
5. Comprehensive Debug Logging
Added 11 debug log statements showing:
Example output from successful execution:
Testing
Function statistics:
Benefits
For macOS Users:
For Maintainers:
For Support:
For macOS Users Experiencing Issues
To diagnose the problem with this PR's changes:
The logs will pinpoint whether the failure is in initialization, stack allocation, memory operations, or computation, making it much easier to identify and fix the root cause.
Addresses
Original prompt
Fixes #46
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.