Skip to content

fix: fix finfo guesser in coroutines#376

Open
albertcht wants to merge 5 commits into0.4from
hotfix/fix-finfo-guesser-coroutine
Open

fix: fix finfo guesser in coroutines#376
albertcht wants to merge 5 commits into0.4from
hotfix/fix-finfo-guesser-coroutine

Conversation

@albertcht
Copy link
Copy Markdown
Member

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
Copy link
Copy Markdown
Member Author

@copilot resolve the merge conflicts in this pull request

Copilot AI and others added 2 commits April 28, 2026 07:37
…er-coroutine

# Conflicts:
#	src/foundation/src/Bus/PendingChain.php
#	src/foundation/src/Support/Providers/RouteServiceProvider.php
#	src/http/src/Client/PendingRequest.php
#	src/http/src/Client/Request.php
#	src/sentry/config/sentry.php
#	src/telescope/config/telescope.php

Co-authored-by: albertcht <9117929+albertcht@users.noreply.github.com>
Co-authored-by: albertcht <9117929+albertcht@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI commented Apr 28, 2026

@copilot resolve the merge conflicts in this pull request

Resolved in 7c0e377. The branch was based on main instead of 0.4, so I merged origin/0.4 into the PR branch and resolved the 7 content conflicts by accepting the 0.4 versions for the unrelated files (from phpstan/cs-fixer commits). The PR diff now cleanly shows only the intended changes: FileinfoMimeTypeGuesser.php, the test file, and the test fixture.

@albertcht albertcht self-assigned this Apr 28, 2026
@albertcht albertcht requested a review from binaryfire April 28, 2026 07:43
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.

2 participants