Taking charge of your race conditions
2022-07-13 , Liffey A

Are you working with threads, processes, or more generally “workers”? And do you have blocks of code that must not be called concurrently? Maybe you didn't even realise it until your system experienced a bug you could not reproduce until the stars aligned. Then you surely know that hope is not the answer to a robust system, we must be prepared to face worst-case scenarios.

This talk will first briefly present race conditions, a staple in concurrent computing. We will then compare implicit and explicit concurrency management in your core logic, that is whether you delegate or craft protective logic yourself. Next comes testing, the real crux of the talk, where we will demonstrate how to manufacture a race condition. Finally we will explore how to solve such problems with the built-in tools the Python standard library offers.


Anybody working with concurrency (threads, processes, or more abstractly workers) eventually encounters a race condition. This kind of hair-pulling problem is a staple in computer science. Sometimes you can offload the problem to a lower layer, usually to the database engine, bundling it with atomic transactions. But there are times when you have to take charge of worst-case concurrency scenarios, when your architecture and constraints do not leave room for implicit concurrency management. Then, you need to carefully craft the paths in and out of the critical section, avoiding deadlocks along the way.

In this talk, we will focus on how to test your program with a manufactured race condition. If you know it can happen, you'd better cover it in your test suite. We will define a server program called in parallel by multiple clients to illustrate the problem at hand. Then, we will manually set-up a race condition during testing to explore broken behaviour and problematic code.

The Python standard library offers all the tools we need to cater for race conditions. We hope the tour we will give allows you to exercise your own race conditions more easily. Why, we even think you will have all the cards in hand to counter them!


Abstract as a tweet:

Get a quick tour of race condition handling in Python: what it is, the built-in tools, and how to test for it.

Expected audience expertise: Domain:

none

Expected audience expertise: Python:

some

See also:

Borjan has been fully focusing his attention on software engineering with Python for the last four years. Prior to that, he was neck deep in the Android world where he was a maintainer of an Android distribution for a phone manufacturer for a few years.
Python came into play there as a nice automation language from applying missing Linux kernel patches to bench testing operating system builds.