当前位置:网站首页>On the problem of class member variable pollution in the context of one-time concurrence

On the problem of class member variable pollution in the context of one-time concurrence

2022-06-21 09:46:00 dddyge

The technical framework of the project I maintain is php Based on swoole One of the web The framework is called swoft. The framework is a collaborative environment , And imitate springboot An annotated php frame . I made a demand , The problem of class member variable pollution in the context of concurrency occurs . The accident is like this : Launch once rpc request , The returned result has a member variable of the class , However, it is found that the requested parameters do not match the results , I checked the error log first , Nothing unusual , Then check the request execution log , It is found that the number of logs is lost . So I looked at the code carefully , I pass the results to the member variables of each class through the responsibility chain pattern , and BeanFactory::getBean By default, this method gets the class of the singleton , So the member variables of the class are polluted by other coroutines without protection . Knowing this question , I modified the annotation , Change each class of the responsibility chain to the multiple instance pattern . Use locally Jmeter The result of pressure measurement is correct , However, a new problem is found, that is, the execution log is lost . I checked it carefully later swoft in log Implementation of package , I found that the framework development team also made the same mistake as me . His idea here is : Store the log in the member variable of the class messages in , This is an array , When the number of elements in this array is greater than the number of disks , Brush the log records in the array into the file . But there is no code block protection for thread synchronization , Therefore, in the case of concurrency, the problem of missing logs caused by overwriting can be reproduced . I was thinking that the development team of the framework should not make such a mistake . So I turned it over github Of swoft Framework of the issue, Found in 20 This question has been raised since , But it has not been repaired . I looked at the source code later , Find him log This bean It's the singleton mode , That's why messages Member variables are polluted , I created a new newLogger Class and set it as the log processing class when the framework starts , The problem of concurrent pollution is solved .

Through the thinking of this accident :1. The framework development team may not be right , Even this github 5k Much of the star There are also a large number of unsolved problems ( Eat the melon : be based on swoole Of web Most of the frameworks seem to follow swoole There is a conflict of interest among the people in the underlying development team, resulting in no one willing to be based on swoole To develop the framework .).2. The framework of mysql Connection pooling is not particularly good , There is no mechanism to keep the connection alive , Often output on the terminal log mysql has gone away Error of , The error is because mysql server The connection was closed , But the code level still regards the connection as a normal connection to send data , So I made such a mistake .

There are two related issue Link to :

https://github.com/swoft-cloud/swoft/issues/1430.

https://github.com/swoft-cloud/swoft/issues/1367. The problem of log duplication can be solved by writing a simple interface , Just print it directly 4 Logs , then 10 Concurrent access by threads , It doesn't have to be 40 Logs are printed , This result is sufficient to prove the problem of CO process pollution class member variables .

原网站

版权声明
本文为[dddyge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206210934266175.html