Skip to content

Conversation

@bismansahni
Copy link
Contributor

@bismansahni bismansahni commented Oct 5, 2025

Summary

Fixes #692

This PR resolves a race condition in ContainersService.create() that occurs when creating containers with the same name concurrently.

Changes

  • Wrapped the entire create() method body with lock.withLock to ensure atomic container creation
  • Added proper await keywords for actor isolation when accessing self.containers within the lock closure
  • Changed direct container state assignment to use setContainerState() with context

Problem

Without the lock, two concurrent create() calls could both pass the existence check at line 133 and proceed to create the same container directory, resulting in filesystem errors like:

Error Domain=NSCocoaErrorDomain Code=516 "vmlinux-6.12.28-153 couldn't be copied to test-race because an item with the same name already exists."

Solution

By wrapping the entire method with lock.withLock, only one container creation can proceed at a time. The second concurrent call will now properly receive:

Error: internalError: "failed to create container" (cause: "exists: "container already exists: test-race"")

This follows the same pattern used in other methods like bootstrap(), startProcess(), and delete().

Testing

bismansahni and others added 2 commits October 4, 2025 17:02
Wrap the entire create() method with lock.withLock to prevent race
conditions when creating containers with the same name concurrently.

Without the lock, two simultaneous create calls could both pass the
existence check and race to create the same directory, resulting in
filesystem errors instead of proper 'container already exists' errors.

Fixes apple#692
@dcantah dcantah merged commit 0d3f20c into apple:main Oct 9, 2025
2 checks passed
Copy link
Contributor

@dkovba dkovba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bismansahni Thank you for the contribution!

Should we do the same in the methods below?

  • createProcess()
  • stop()
  • delete()
  • _cleanup()

saehejkang pushed a commit to saehejkang/container that referenced this pull request Oct 9, 2025
Fixes apple#692 

This PR resolves a race condition in `ContainersService.create()` that
occurs when creating containers with the same name concurrently.
Ronitsabhaya75 pushed a commit to Ronitsabhaya75/container that referenced this pull request Oct 16, 2025
Fixes apple#692

This PR resolves a race condition in `ContainersService.create()` that
occurs when creating containers with the same name concurrently.
Ronitsabhaya75 pushed a commit to Ronitsabhaya75/container that referenced this pull request Oct 16, 2025
Fixes apple#692

This PR resolves a race condition in `ContainersService.create()` that
occurs when creating containers with the same name concurrently.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Race conditions in ContainersService

3 participants