Skip to content

fix: fix finfo guesser in coroutines#375

Merged
albertcht merged 21 commits into
0.3from
hotfix/fix-finfo-guesser-coroutine
Apr 28, 2026
Merged

fix: fix finfo guesser in coroutines#375
albertcht merged 21 commits into
0.3from
hotfix/fix-finfo-guesser-coroutine

Conversation

@albertcht
Copy link
Copy Markdown
Member

@albertcht albertcht commented Apr 28, 2026

Problem

FileinfoMimeTypeGuesser cached finfo instances in a static class property($finfoCache). In a coroutine environment this is unsafe: a single finfo resource held in a static array is shared across all coroutines, which can lead to race conditions and undefined behavior when multiple coroutines concurrently call guessMimeType().

Solution

Replace the static array cache with Context::getOrSet(), which stores the finfo instance in Hypervel's coroutine context. Each coroutine gets its own isolated finfo instance, eliminating the shared-state problem while preserving the caching benefit within a single coroutine's lifetime.

The cache key is built from a fixed prefix plus the optional magic file path, matching the previous per-magic-file granularity:

// Before
private static $finfoCache = [];
$finfo = self::$finfoCache[$this->magicFile] ??= new finfo(FILEINFO_MIME_TYPE, $this->magicFile);

// After
private const FINFO_CACHE_KEY = '__support.finfo_mime_type_guesser.';
$finfo = Context::getOrSet(
    self::FINFO_CACHE_KEY . ($this->magicFile ?? ''),
    fn () => new finfo(FILEINFO_MIME_TYPE, $this->magicFile)
);

@albertcht albertcht added the bug Something isn't working label Apr 28, 2026
@albertcht albertcht merged commit f2f1097 into 0.3 Apr 28, 2026
17 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants