Skip to content

[BUG] SessionSharing doesn't work on Windows - shared memory destroyed after handle closed #573

@swatmaps

Description

@swatmaps

Describe the bug

SessionSharingThread creates shared memory but immediately closes the handle in _share_token(). On Windows, shared memory is destroyed when all handles are closed, so worker processes cannot access it.

To Reproduce

Environment

  • OS: Windows 10/11
  • Python: 3.12
  • sentinelhub-py: 3.11.x

Steps to Reproduce

from concurrent.futures import ProcessPoolExecutor
from sentinelhub import SentinelHubSession, SHConfig
from sentinelhub.download import SessionSharing, collect_shared_session

def worker():
    session = collect_shared_session()
    return session.token["access_token"][:20]

config = SHConfig()
# config.sh_client_id = "..."
# config.sh_client_secret = "..."

session = SentinelHubSession(config)

with SessionSharing(session), ProcessPoolExecutor(max_workers=1) as executor:
    future = executor.submit(worker)
    print(future.result())

Expected Behavior

Worker processes should be able to access the shared session token.

Actual Behavior

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'sh-session-token'

Root Cause

In _share_token(), the shared memory handle is closed after writing:

def _share_token(self, token: JsonDict) -> None:
    encoded_token = json.dumps(token).encode()
    memory = self._get_shared_memory(encoded_token)

    try:
        memory.buf[:] = encoded_token + _NULL_MEMORY_VALUE * (memory.size - len(encoded_token))
    finally:
        memory.close()  # <-- This destroys the memory on Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions