Better Programming

Advice for programmers.

Member-only story

Handling Actor Reentrancy Problem in Swift

6 min readSep 22, 2021

--

Photo by Mateusz Wacławek on Unsplash

When the first time I saw the WWDC presentation about actors, I was thrilled with what it is capable of and how it will change the way we write asynchronous code in the near future. By using actors, writing asynchronous code that is free from data races and deadlocks has never been easier.

All that aside, that doesn’t mean that actors are free from threading issues. If we are not careful enough, we might accidentally introduce a reentrancy problem when using actors.

In this article, I am going to walk you through what is a reentrancy problem, why it is problematic, and how you can prevent it from happening. If this is the first time you heard of the reentrancy problem, definitely read on so that it won’t catch you off guard next time when you use actors.

A Real-life Bank Transaction Example

The best way to showcase the reentrancy problem is by using a real-life example. Consider the following BankAccount actor that has a balance variable.

--

--

Lee Kah Seng
Lee Kah Seng

Written by Lee Kah Seng

Support me by becoming a Medium member: https://leekahseng.medium.com/membership ⦿ 🇲🇾 Creator of https://swiftsenpai.com ⦿ iOS developer since 2011