Global shoot in the foot
8, Apr, 2019What can go wrong with globals? A short story of one event..
First there was an single task app, a server. It run on single data. The service relay on a characteristic parameter - the world size. It is passed by client to the server on the initialization of connection.
Next design iteration. The client-server approach is too slow. Change the interface and make it a library. The library will be intialized with the parameter. We are good. Still we use on single data at a time.
Its refactor time, make an easy api. - Initialization will be automatic on incoming data. World size set by heuristic. Oh no, we don’t have time for internals cleanup, global stays.
Some time after. Hey, we have more items which can be processed. Make init/do the thing with the lib in the loop. Looks good, works well. We are still single threaded.
Time passes. We have so many items to be processed. It takes long. Let’s speed up it a little. Run the library procedure for each item in separate thread. Few lines of code and looks good. Tests passed. Push to master.
Few months later - hey I run the tasks for multiple data at once. The results seems to be corrupted… And of course nobody remembered the global. It looked like array out of bound access - but valgrind found nothing. Sometimes crashed - maybe double free? A day later…
- Posted by michal in
- Add a comment