Commit 48ae7af
committed
ngx_cache_purge: fix correctness, safety, and build issues
Fix C89/gnu89 compliance by hoisting all mid-scope variable declarations
to the top of their enclosing function scope in the fastcgi, proxy, scgi,
and uwsgi purge handlers. Affected variables: deleted (all four handlers),
cv_val, i, name, caches (proxy), wctx (two walk callbacks).
Fix cache_purge_batch_size being accepted and stored but never consulted.
Rewrite process_queue() to loop over queue->batch_size items per call.
Returns NGX_AGAIN after a full batch, NGX_OK when the queue empties early.
Fix live reload silently discarding changes to batch_size, throttle_ms,
and queue_size. On reload init_shm_zone() now acquires queue->mutex and
writes the new values into the running queue. max_size is floor-bounded
by the current queue depth to prevent the queue appearing full mid-reload.
Fix vary_aware variant deletion leaving stale shared-memory metadata.
Previously delete_exact_file() called ngx_delete_file() directly, leaving
each variant's rbtree node with exists=1 and a non-zero fs_size, causing
stale HIT responses and inflated cache->sh->size accounting until restart.
Add ngx_cache_purge_invalidate_node() which decodes the 16-byte MD5 cache
key from the last 32 hex characters of the file path, performs an O(log n)
rbtree lookup, and clears fcn->exists, fcn->fs_size, and sh->size under
shpool->mutex before the file is removed. Add a cache pointer to the walk
context so the callback can reach the cache object. Lock ordering is
preserved throughout: queue_mutex before shpool_mutex.
Fix integer overflow in shm_size arithmetic on 32-bit builds. Add
NGX_CACHE_PURGE_QUEUE_SIZE_MAX (65535) and reject queue_size=0 and
batch_size=0 in init_main_conf; both values make the queue unusable in
different ways.
Fix data race on queue->size in the enqueue debug log path. The field
was read after ngx_shmtx_unlock(). Capture the value into a local
variable while the mutex is held and use that snapshot in the log call.
Fix queue->size declared as ngx_atomic_t despite always being accessed
under queue->mutex. Change to ngx_uint_t and remove the resulting
(ngx_uint_t) casts at all usage sites.
Fix send_response() body length formula. Remove two redundant aliases
(resp_tmpl_len, len) that were always equal to body_len. Replace the
opaque "- 2 - 2 - 1" expression with (resp_body_size - 1 - 4) and add a
comment explaining each term.
Replace the magic constant 6 used as the KEY: header offset in two places
with NGX_CACHE_PURGE_KEY_HDR_OFFSET and a comment explaining the layout.
Mark all eight response content-type size globals const.
Add a NULL guard for cplcf->conf at the top of the access handler before
its first dereference.
Fix the config script shebang from bash to sh. The file is dot-sourced
by nginx's auto/configure; on Debian and Ubuntu /bin/sh is dash. Remove
the heredoc that wrote to $NGX_AUTOCONF_ERR; that file is reserved for
the nginx build system. Write the status message to stdout instead.
Fix non-existent GitHub Actions version pins. All four references were
one or two major versions ahead of any released tag and the pipeline
failed to resolve them: actions/checkout v5->v4, setup-buildx-action
v4->v3, build-push-action v7->v6, upload-artifact v7->v4. Fix cppcheck
--std=c89 to --std=c11; nginx's build system uses -std=gnu89 and cppcheck
has no gnu89 mode.
Add basic.t TEST 15 covering the vary_aware directory walk. The test
pre-seeds two fake variant files with the same KEY: line at different
shard paths and verifies that a single PURGE request removes both. This
code path had no test coverage.
Rewrite the background_handler and process_queue block comments which
described processing one item per invocation after the batch loop was
introduced.1 parent 4e012c1 commit 48ae7af
3 files changed
Lines changed: 552 additions & 181 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
2 | 15 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
7 | 20 | | |
8 | 21 | | |
9 | 22 | | |
| |||
20 | 33 | | |
21 | 34 | | |
22 | 35 | | |
23 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
24 | 39 | | |
25 | 40 | | |
26 | 41 | | |
27 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
28 | 50 | | |
29 | | - | |
30 | 51 | | |
31 | 52 | | |
32 | 53 | | |
33 | | - | |
34 | | - | |
35 | 54 | | |
36 | | - | |
37 | | - | |
38 | 55 | | |
39 | | - | |
40 | | - | |
41 | 56 | | |
42 | 57 | | |
43 | | - | |
44 | 58 | | |
45 | 59 | | |
46 | 60 | | |
47 | 61 | | |
48 | | - | |
| 62 | + | |
49 | 63 | | |
50 | 64 | | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
| 65 | + | |
0 commit comments