fix(static): skip magic-number pread under directio (O_DIRECT EINVAL)#75
Merged
Conversation
When "directio <size>" is configured, ngx_open_cached_file() opens the .zst with O_DIRECT once the file meets the threshold. The static handler's 4-byte magic-number sanity probe then issues a deliberately tiny, unaligned pread() — but an O_DIRECT read requires buffer, offset and length all aligned to the device block size, so the probe fails EINVAL on every request. The handler treated that as a failed read and declined, wrongly refusing to serve every .zst above the directio threshold and spamming NGX_LOG_CRIT once per request. The probe is only a best-effort corruption guard, so skip it when of.is_directio: the precompressed file is still served, just without the magic sanity check (which O_DIRECT alignment makes impossible to run cheaply here anyway). Test (t/01-static.t TEST 26): zstd_static on + directio 1 serves the .zst with 200 + Content-Encoding: zstd and no [error]/CRIT. O_DIRECT is filesystem-dependent, so the test deterministically catches the declined+CRIT regression where O_DIRECT engages and still asserts correct serving elsewhere. Verified to fail on pre-fix code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MAJOR from module audit.
Problem
With
directio <size>configured,ngx_open_cached_file()opens the.zstwithO_DIRECTonce the file meets the threshold. The static handler then runs its 4-byte magic-number sanity probe with an unalignedpread()— but anO_DIRECTread requires buffer, offset and length all aligned to the device block size, so the probe fails EINVAL on every request. The handler treated that as a failed read → declined every.zstabove the directio threshold and spammedNGX_LOG_CRITonce per request.Fix
Skip the probe when
of.is_directio. It is only a best-effort corruption guard; the precompressed file is still served, just without the magic check (which O_DIRECT alignment makes impossible to run cheaply here anyway).Test
t/01-static.tTEST 26 —zstd_static on; directio 1;serves the.zstwith200+Content-Encoding: zstd, no[error]/CRIT. O_DIRECT is filesystem-dependent, so the test deterministically catches the declined+CRIT regression where O_DIRECT engages and still asserts correct serving elsewhere. Verified to fail on pre-fix code.Local:
prove t/01-static.t= 303/303 PASS.