79110791

Date: 2024-10-21 15:51:14
Score: 1
Natty:
Report link

Yep. you need to serialize those operations. Even though you're using std::atomic for is_shutdown, there's still a race condition.

This is what can happen:

Thread t2 checks is_shutdown and sees it's false (#2).

Before t2 calls use(fd), thread t1 sets is_shutdown to true and closes fd (#1).

Now t2 calls use(fd) (#3) on a closed fd.

This means t2 might use fd after it's closed, which can cause errors. The check (is_shutdown.load()) and the action (use(fd)) are not happening together atomically.

You should use a mutex or some synchronization to make sure that when one thread is using fd, the other isn't closing it. That way either the thread sees that shutdown has happened and doesn't use fd, or it safely uses fd without worrying about it being closed.

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Low reputation (1):
Posted by: CodingCossack