Skip to content

Fix/ttl segfault issue#18

Merged
RyanKung merged 5 commits into
masterfrom
fix/ttl-segfault-issue
Dec 3, 2025
Merged

Fix/ttl segfault issue#18
RyanKung merged 5 commits into
masterfrom
fix/ttl-segfault-issue

Conversation

@RyanKung

@RyanKung RyanKung commented Dec 3, 2025

Copy link
Copy Markdown
Contributor

No description provided.

- Add ttl_config_tests module to detect segfaults
- Test TTL configuration doesn't cause worker process crashes
- Test TTL configuration merging from parent to child locations
- Test multiple requests with TTL configuration
- Add x402_ttl 60 to test configuration

This addresses the segfault issue reported where worker processes
exit with signal 11 (core dumped) after TTL configuration was added.
… pool

The segfault was caused by directly copying ngx_str_t pointers from
prev_conf to conf_mut in merge_loc_conf. When parent and child
configurations use different memory pools, copying pointers can lead
to dangling pointers that cause segfaults when accessed later.

Fix:
- Use copy_string_to_pool() to reallocate strings from the current
  configuration pool instead of copying pointers
- Apply this fix to all string fields: amount_str, pay_to_str,
  facilitator_url_str, description_str, network_str, network_id_str,
  resource_str, asset_str, asset_decimals_str, timeout_str,
  facilitator_fallback_str, and ttl_str
- Export common module to make copy_string_to_pool accessible

This ensures all strings are allocated from the current configuration's
memory pool, preventing segfaults when accessing configuration values
during request processing.
Add comprehensive panic handling to prevent panics from crossing FFI
boundaries, which would cause undefined behavior. This provides detailed
logging when panics occur to help with debugging.

Changes:
- Create panic_handler module with panic protection utilities:
  - init_panic_hook(): Initialize global panic hook for FFI safety
  - catch_panic(): Catch panics and log detailed information
  - catch_panic_or_default(): Catch panics and return default value
  - catch_panic_with_info(): Catch panics with additional context
- Wrap all FFI functions with panic protection:
  - x402_phase_handler: Protected with catch_panic_or_default
  - x402_ngx_handler: Protected with catch_panic_or_default
  - x402_metrics_handler: Protected with catch_panic_or_default
- Auto-initialize panic hook in logging module
- Add Safety documentation to copy_string_to_pool function

Benefits:
- Prevents undefined behavior from panics crossing FFI boundaries
- Provides detailed panic logs including message, file location, and context
- Helps debug issues in production by logging panics to nginx error log
- All panics are caught and logged before they can crash nginx worker processes
Apply DRY principle to reduce code duplication in configuration merging.

Changes:
- Create merge_string_field! macro to handle string field merging
- Replace 60 lines of repetitive code with 12 macro calls
- Net reduction: 28 lines of code
- Improve maintainability: changes to merge logic only need to be made once

Benefits:
- Follows DRY (Don't Repeat Yourself) principle
- Easier to maintain: single point of change for merge logic
- Easier to extend: adding new fields requires only one macro call
- More readable: clear intent with concise syntax
@RyanKung RyanKung merged commit 1b6f2e8 into master Dec 3, 2025
7 checks passed
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.

1 participant