Location>code7788 >text

Can I still receive TCP connection requests after the * exception of the .NET process?

Popularity:977 ℃/2025-02-09 10:42:57

Yesterday, several processes on the line were Crashed because of *Exception, but TCP requests can still be connected. Can you connect to a microservice application process with *Exception?

Do a research and share:

Occurs in the .NET process*Exceptionafter,Usually, it is not possible to continue receiving TCP connection requests, the reasons are as follows:

  1. *ExceptionNot captured by default

    • In .NET Core and .NET 5+,*Exception Unable to betry-catchcapture, Once it happens,The process will crash directly
    • In the .NET Framework (such as ), even if it can be passedmonitor,The process may still enter an unstable state, it is difficult to guarantee that network requests will continue to be processed.
  2. Thread stack overflow causes process to crash

    • *ExceptionWhen it happens, it usually meansThe stack space has been exhausted(such as recursion too deep, infinite recursion, etc.).
    • Since TCP connections usually depend onThreadPoolthread orasync/awaitTask Scheduling, Once*Exceptiontrigger,The entire process may crash and all connections cannot continue to be processed
  3. Possibility in special circumstances

    • if*ExceptionOnly happens inSingle thread(non-main thread or mission-critical thread), and the application does not crash, it is possible to continue receiving TCP connections.
    • But this depends extremely on the application architecture, and under .NET Core/.NET 5+,The process will basically crash directly

How to prevent*ExceptionImpact TCP connections?

  1. Avoid recursion causing stack overflow(If usedwhileinstead of recursion, or control the depth of recursion).
  2. useThreadPoolQuarantine mission, try to avoid core threads (such asMain()Execution in threads may cause*Exceptioncode.
  3. Enable process monitoring(likesupervisorsystemdorKubernetes), once the process crashes,Automatically pull up new processes, resume services as soon as possible.

In modern .NET runtimes (.NET Core and .NET 5+),*Exception Usually causes the process to crash, the TCP server cannot continue to accept connections.

If under the .NET Framework, and*ExceptionOnly affect non-core threads, in theory, TCP listening may continue to work, but there are still great risks.