Skip to content

Local storage does not serialize concurrent uploads from separate threads #2250

Backlog

Description

@khushalx

Two uploads to the same local object can enter their critical sections together when they run in different threads. upload_object and upload_object_via_stream create separate LockLocalStorage instances, each with its own threading.Lock. On POSIX, the file lock does not exclude another thread in the same process.

Reproduced on current trunk (b2e794d), Python 3.12, macOS:

import tempfile
import threading
from libcloud.storage.drivers.local import LockLocalStorage

with tempfile.TemporaryDirectory() as directory:
    first = LockLocalStorage(directory + "/object")
    second = LockLocalStorage(directory + "/object", timeout=0.1)
    entered = threading.Event()

    def writer():
        with second:
            entered.set()

    with first:
        thread = threading.Thread(target=writer)
        thread.start()
        thread.join(2)
        print(entered.is_set())  # True: both writers hold the lock
    thread.join()

The second writer should wait or time out until the first releases the lock. Concurrent stream uploads can otherwise overwrite each other's bytes.

A related ordering problem is that __exit__ releases the thread lock before the file lock. Both locks need to cover the same critical section.

AI disclosure: Codex reproduced this bug and prepared the regression tests and proposed fix.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions