Skip to content

[wip]refactor: introduce AppContext to replace global CancellationToken#1079

Draft
Itsusinn wants to merge 2 commits intomasterfrom
refactor-context-shutdown
Draft

[wip]refactor: introduce AppContext to replace global CancellationToken#1079
Itsusinn wants to merge 2 commits intomasterfrom
refactor-context-shutdown

Conversation

@Itsusinn
Copy link
Copy Markdown
Member

What does this PR do?

  • Removes the global SHUTDOWN_TOKEN static variable and shutdown() function from clash-lib/src/lib.rs.
  • Introduces an AppContext struct in clash-lib/src/app/context/mod.rs to properly manage lifecycle and cancellation signals across components.
  • Propagates AppContext via dependency injection to core runtime components (InboundManager, TunRunner, DnsRunner, ApiRunner), replacing their internal standalone cancellation tokens.
  • Replaces multiple instances of the custom print_and_exit! macro and direct std::process::exit calls (e.g. in config rule parsing, resolver initialization, and outbound manager) with returning Result::Err(crate::Error) for graceful error propagation, which is vital for using clash-lib as a robust library.

This provides a cleaner, more idiomatic architecture that prevents sudden process crashes on configuration errors and gives the library consumer full control over component lifecycle.

@Itsusinn Itsusinn force-pushed the refactor-context-shutdown branch from a1c83b9 to 795ddfe Compare March 27, 2026 08:45
@github-actions
Copy link
Copy Markdown
Contributor

🚀 TUN Throughput Benchmark Results

📊 Test Environment
Component Details
OS Linux 6.14.0-1017-azure
Architecture x86_64
CPU AMD EPYC 7763 64-Core Processor
CPU Cores 4
Memory 15.62 GB
Kernel 6.14.0-1017-azure
Network Interfaces eth0 (50000 Mbps), docker0, enP52666s1 (50000 Mbps)

❌ Benchmark Failed

The TUN benchmark test failed to complete. Check the workflow logs for details.

Baseline test succeeded:

  • Throughput: 34.79 Gbps

Common causes:

  • TUN device initialization failure
  • Network configuration issues
  • Timeout during iperf3 test

🤖 Benchmark run on GitHub Actions CI •
Test duration: 10s • Linux x86_64 • 4 cores

@Itsusinn Itsusinn enabled auto-merge (squash) March 27, 2026 11:31
@Itsusinn Itsusinn disabled auto-merge March 27, 2026 11:31
@Itsusinn Itsusinn changed the title refactor: introduce AppContext to replace global CancellationToken [wip]refactor: introduce AppContext to replace global CancellationToken Apr 6, 2026
@Itsusinn Itsusinn marked this pull request as draft April 6, 2026 05:06
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