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*Exception
after,Usually, it is not possible to continue receiving TCP connection requests, the reasons are as follows:
-
*Exception
Not captured by default- In .NET Core and .NET 5+,
*Exception
Unable to betry-catch
capture, Once it happens,The process will crash directly。 - In the .NET Framework (such as ), even if it can be passed
monitor,The process may still enter an unstable state, it is difficult to guarantee that network requests will continue to be processed.
- In .NET Core and .NET 5+,
-
Thread stack overflow causes process to crash
-
*Exception
When it happens, it usually meansThe stack space has been exhausted(such as recursion too deep, infinite recursion, etc.). - Since TCP connections usually depend on
ThreadPool
thread orasync/await
Task Scheduling, Once*Exception
trigger,The entire process may crash and all connections cannot continue to be processed。
-
-
Possibility in special circumstances
- if
*Exception
Only 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。
- if
How to prevent*Exception
Impact TCP connections?
-
Avoid recursion causing stack overflow(If used
while
instead of recursion, or control the depth of recursion). -
use
ThreadPool
Quarantine mission, try to avoid core threads (such asMain()
Execution in threads may cause*Exception
code. -
Enable process monitoring(like
supervisor
、systemd
orKubernetes
), once the process crashes,Automatically pull up new processes, resume services as soon as possible.