@neralabs/rerail-pack-celery 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,462 @@
1
- # Temporary Holding Version
1
+ # Rerail rule pack: Celery
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Rerail's own Celery rules, from the Celery docs and maintainers' review advice, for idempotent, serializable tasks dispatched after commit.
4
+
5
+ ## Rules
6
+
7
+ In the order a team should read them: what it loses when the rule is missing comes first.
8
+
9
+ | Rule | Mode | Catches | Corpus matches (repos) | Read precision |
10
+ |---|---|---|---|---|
11
+ | `celery-practice-dispatch-before-commit` | Steer | Celery practice: a task sent between a SQLAlchemy write and its commit | 3 (2) | 100% (3 of 3) |
12
+ | `celery-practice-pickle-serializer` | Steer | Celery practice: Celery set to send or accept pickle messages | 9 (2) | 100% (9 of 9) |
13
+ | `celery-practice-countdown-past-visibility-timeout` | Steer | Celery practice: a task delayed past the broker's redelivery limit | 4 (4) | 100% (4 of 4) |
14
+ | `celery-practice-result-get-in-task` | Steer | Celery practice: a task waits on another task's result | 0 | no match (cases and counterexamples only) |
15
+ | `celery-practice-task-arg-not-serializable` | Steer | Celery practice: a SQLAlchemy model or session, a set, a file, a lambda or a function passed as a Celery task argument | 0 | no match (cases and counterexamples only) |
16
+ | `celery-practice-unknown-task-name` | Log | Celery practice: task named by a string that no task of the run registers | 0 | no match (cases and counterexamples only) |
17
+
18
+ Each rule's `.test cases exactly` block reads its cases, one small repository per rule, from
19
+ `upstream/celery-practice/authored/<id>/`: pack test data in S3 (`npm run fixtures:pull -- --pack celery`), not in git.
20
+
21
+ ## Rule details
22
+
23
+ ### `celery-practice-dispatch-before-commit`
24
+
25
+ Rerail's own rule, not a port: the SQLAlchemy (and plain-session) side of `django-practice-enqueue-on-commit`. A
26
+ Celery task sent while the session holds uncommitted writes can start on a worker before the commit lands: the worker
27
+ finds no row (or the old one), or acts on a change that then rolls back.
28
+
29
+ A send is `t.delay(...)`/`t.apply_async(...)` on a Celery task, resolved through imports (`lib/celery`'s
30
+ `cel_task_ref`: a task def of the run, also under an alias or a re-export, a task object such as `app.task(fn)` or a
31
+ `Task` subclass instance, or a signature, `t.s(...)` or `signature("name")`), `<x>.send_task(...)`, or a call of a
32
+ function of the run that sends, directly or through the call graph, where no function along the way commits before
33
+ its send; such a call is reported at the call, naming the helper. RQ's `@job`, a thread pool and any other object
34
+ with a `delay` method are not sends. A write is a session call (`add`, `add_all`, `delete`, `merge`, `flush`,
35
+ `bulk_*`, `execute` of an `insert`/`update`/`delete`), or an assignment to a mapped column of a row the function loaded
36
+ through that session (`s.get(M, pk)`, `s.query(M)...first()`, `s.scalar(select(M))`, `s.scalars(select(M))`, a `for`
37
+ over them; columns resolved through imports and base classes: `mapped_column`, `Column`, `relationship`, SQLModel's
38
+ `Field`, `Mapped[...]`), which the session flushes at commit. A session is `lib/celery`'s `sa_session`: proven by what
39
+ binds it (a session class's constructor, a `sessionmaker`/`scoped_session`, Flask-SQLAlchemy's `db.session`, a run
40
+ function returning one, an annotation), else an unannotated name ending in `session` in a run that imports
41
+ SQLAlchemy. A write and a send pair only when one path runs both in that order: not in different arms of one
42
+ `if`/`elif`/`else`, `match`, or `try`'s `except`/`else`, and not past a `return` or `raise` that ends the write's
43
+ block. The rule reports a send
44
+
45
+ 1. that follows a write in the same function, with a `commit()` of the same session after it and no commit between
46
+ them on that path;
47
+ 2. in the body of `with <x>.begin():`, `with sessionmaker.begin() as s:` or `async with`, after a write in that
48
+ body on the block's session: the block commits only when it ends;
49
+ 3. in a function that writes, then sends, and never commits, when a caller (through any number of callers that do
50
+ not commit either) commits the same session after the call, or makes the call inside a `begin()` block on it. The
51
+ session is followed from the callee's parameter to the caller's argument (by keyword, position, and the `self`
52
+ offset of a bound method). This is dify's `DatasetService._update_internal_dataset`:
53
+ `session.execute(update(Dataset)...)`, `session.flush()`, then `deal_dataset_vector_index_task.delay(...)`, with
54
+ the commit left to the caller's `with Session(...)` block;
55
+ 4. in a flush-time SQLAlchemy event listener, `@event.listens_for(X, "<event>")` or `event.listen(X, "<event>", fn)`
56
+ for `before_insert`, `after_insert`, `before_update`, `after_update`, `before_delete`, `after_delete`,
57
+ `before_flush`, `after_flush`, `after_flush_postexec` and `before_commit`: these run inside the flush or before
58
+ the commit.
59
+
60
+ The message names the send (or the helper) and which of these it is, and tells how to fix it: send after `commit()`,
61
+ or from an `after_commit` event, and to leave it when the task reads nothing written there.
62
+
63
+ It leaves alone: a send after the commit with no write in between; a write after the send; a write and a send in
64
+ sibling branches (`if is_new: session.add(o)` / `else: ship.delay(o.id)`); a helper that commits before its own send;
65
+ a send in an `after_commit` listener, or in a lambda or nested def; a send in an except handler that always re-raises;
66
+ `r = t.apply_async(...)` whose result the function reads afterwards (the task id is stored on the row); `.delay()` on
67
+ anything not resolved to a Celery task; a caller that commits a different session than the one the helper wrote; a
68
+ name bound to an HTTP client (`requests.Session()`, an `aiohttp.ClientSession` annotation); test files; and documents
69
+ that import django (the django pack's three dispatch rules own those).
70
+
71
+ **Gaps:** loops, where a write later in the body precedes the next iteration's send; writes through repository
72
+ objects, or to a row loaded elsewhere and passed in; a Flask `Model.query.get()` row (no session spelled); a
73
+ transaction a framework opens and commits around the request (Flask-SQLAlchemy teardown, superset's `@transaction()`
74
+ decorator, indico's request handler); a caller's commit reached only through a call the call graph cannot resolve; a
75
+ commit in only some arms between the write and the send counts as a commit between them; an unannotated parameter
76
+ named like a session (`http_session`) is taken for a SQLAlchemy session, the fallback where nothing resolves.
77
+
78
+ **Overlap:** none. Ruff, Bandit and pylint have no Celery or SQLAlchemy transaction checks; the django pack's
79
+ `enqueue-on-commit`, `dispatch-in-atomic-request` and `task-arg-model-instance` read Django's ORM only.
80
+
81
+ **Corpus:** 3 matches, all read, all real (precision 3/3, a small sample). Re-measured after the rework with the final
82
+ rule (runs `celery-final-*`, on the Junction corpus boxes, which take repositories under 1,000 .py files):
83
+ Netflix/lemur (`3a3c2c2`, 337 files), flaskbb/flaskbb (`63afaa4`, 242), zenodo/zenodo-rdm (`b5bf82e`, 264),
84
+ makeplane/plane (`723bb17`, 784) and Buuntu/fastapi-react (`af17f2d`, 30): 0 findings. Not re-run (5,000 and 3,000
85
+ files, over the boxes' limit), with the review's figures: langgenius/dify at `724ac15121ee`: 2,
86
+ `api/services/knowledge/dataset_service.py:755` and `:758`, `deal_dataset_vector_index_task.delay` and
87
+ `regenerate_summary_index_task.delay` after `session.execute(update(Dataset)...)` and `session.flush()` in
88
+ `_update_internal_dataset`, whose caller's `with Session(...)` block commits after the call; both tasks load the
89
+ dataset row the update changed. apache/superset at `fdf61721d056`: 1, `superset/models/slice.py:656`,
90
+ `cache_chart_thumbnail.delay` in `event_after_chart_changed`, registered for `after_insert` and `after_update` on
91
+ `Slice`. Both stayed reported in the rule writer's run of the first rework (path arms, `celery-fix-dbc-*`), before
92
+ the engine moved off the laptop. On the review's counterexamples (`celery-final-cx-dbc`): 5 findings, 4 real
93
+ (`flaskapp.py:11`, `orders.py:32` the attribute write, newly caught, `:54`, `:63`); the fifth is the contrived
94
+ unannotated `http_session` (`orders.py:24`), the name fallback; the review's if/else false positive, RQ and the pool
95
+ are silent (review: 6 findings, 3 real).
96
+
97
+ **Sources:**
98
+ - Celery docs, [Tasks: Database transactions](https://docs.celeryq.dev/en/stable/userguide/tasks.html#database-transactions):
99
+ "If the asynchronous task starts executing before the transaction is committed, it may attempt to query the
100
+ article object before it exists. To prevent this, we need to ensure that the transaction is committed before
101
+ triggering the task."
102
+ - Adam Johnson, [Common Celery issues on Django projects](https://adamj.eu/tech/2020/02/03/common-celery-issues-on-django-projects/):
103
+ "If you enqueue a task from within a database transaction, it might execute before the database commits the
104
+ transaction."
105
+ - BaoDev, [flush vs commit: the sqlalchemy write that isn't saved yet](https://www.baodev.studio/blog/sqlalchemy-flush-vs-commit-difference):
106
+ "flush sends your INSERT to the database. commit makes it real for everybody else"; in the gap a Celery worker
107
+ looked for the flushed order, found nothing, and raised `NoResultFound`.
108
+ - Steve Dignam, [The Many Problems with Celery](https://steve.dignam.xyz/2023/05/20/many-problems-with-celery), "No
109
+ transactional job enqueuing": you can't enqueue a job inside a transaction.
110
+
111
+ ### `celery-practice-pickle-serializer`
112
+
113
+ Rerail's own rule, not a port. A worker that accepts pickle unpickles whatever a message on the broker holds, so anyone
114
+ who can write to the broker (a shared Redis, a leaked URL) runs code in every worker that takes the message. Celery 4.0
115
+ made JSON the default for that reason.
116
+
117
+ It matches, at the value that names pickle (`"pickle"` or its MIME type `"application/x-python-serialize"`, a list,
118
+ tuple or set holding one, or a module constant of the same file that holds either):
119
+
120
+ - the settings `task_serializer`, `result_serializer` and `event_serializer` set to pickle, and `accept_content` or
121
+ `result_accept_content` holding it, in every spelling `lib/celery`'s `cel_setting` reads: `app.conf.x = v`,
122
+ `app.conf["x"] = v`, `app.conf.update(x=v)` and `app.conf.update({"x": v})`, `Celery(..., x=v)`, a Django setting
123
+ `CELERY_X = v`, and bare keys in a class or module named for Celery (`class CeleryConfig:`, `celeryconfig.py`);
124
+ - a task's own option, `@app.task(serializer="pickle")` or `@shared_task(serializer=...)`, and `serializer =
125
+ "pickle"` in the body of a class deriving (through the run's classes) from celery's `Task`;
126
+ - a send's option on a Celery task or any `send_task`: `t.apply_async(..., serializer="pickle")` (`t` resolving to a
127
+ Celery task) and `app.send_task(..., serializer="pickle")`.
128
+
129
+ It leaves alone: other serializer names, among them custom signed ones (paperless-ngx registers an HMAC-signed
130
+ `"signed-pickle"` and accepts `"application/x-signed-pickle"`); `"json"`, `"msgpack"` and `"yaml"`; every run that
131
+ calls `app.setup_security(...)` outside tests (Celery then sends and accepts only signed messages); test code and test
132
+ settings files (`test*.py`, `*_test*.py`, and the pack's test paths); a bare `task_serializer =` outside a class or
133
+ module named for Celery; `serializer="pickle"` on calls that are not Celery sends (a cache client's
134
+ `set(..., serializer=...)`, a multiprocessing pool's or another library's `apply_async`, kombu's `publish`).
135
+
136
+ **Gaps:** values read from the environment (`os.environ.get("CELERY_TASK_SERIALIZER", "pickle")`) or from another
137
+ module's constant; a serializer registered with kombu under another name that wraps pickle; `options={"serializer":
138
+ ...}`, `.set(serializer=...)` on a signature; a configuration dict passed to `config_from_object`, and
139
+ `app.conf.update(**{...})`.
140
+
141
+ **Overlap:** Bandit's B301 and Ruff's S301 (`bandit-b301-pickle` in the python pack) flag `pickle.load(s)` calls only;
142
+ no linter reads Celery's configuration.
143
+
144
+ **Corpus:** 9 matches in 2 of 20 Celery repositories, all 9 real (100%). taigaio/taiga-back (`8ab6a4e`, 933 files,
145
+ cloned for this rule): `settings/common.py:53`, `:54`, `:55` and `docker/config.py:112`, `:113`, `:114`,
146
+ `CELERY_ACCEPT_CONTENT = ['pickle', ]`, `CELERY_TASK_SERIALIZER = "pickle"`, `CELERY_RESULT_SERIALIZER = "pickle"`
147
+ in the settings it deploys, with no `setup_security()` (the same six with the reworked library, run
148
+ `celery-fix-three-taigaio__taiga-back`). indico/indico (`8e64139`, 1,301 files, not re-run; the review's figures):
149
+ `indico/core/celery/core.py:66`, `:67`, `:68`, `result_serializer`, `task_serializer` and `accept_content` set to
150
+ pickle on purpose ("that way we can pass along all types"), still the exposure the security guide names. No match in
151
+ paperless-ngx/paperless-ngx (its `"signed-pickle"` settings stay silent), makeplane/plane, pretix/pretix,
152
+ mozilla/addons-server, Netflix/lemur, flaskbb/flaskbb, zenodo/zenodo-rdm and Buuntu/fastapi-react (re-run after the
153
+ rework), nor in saleor, DefectDojo (serializer from the environment, default `"json"`), superset or dify (review).
154
+ Both repositories with matches pass objects to tasks by design, so `setup_security()` is the realistic fix there.
155
+ On the review's counterexamples (`celery-final2-cx-pickle`): 1 finding, `settings/dev.py:2` (pickle in a development
156
+ settings file, debatable as the review says); the pool's `apply_async(serializer="pickle")` false positive is gone,
157
+ and the spellings the review listed (dict passed to `config_from_object`, environment defaults, imported constants,
158
+ `**{...}` updates, `.set(serializer=...)`) stay documented gaps.
159
+
160
+ **Sources:**
161
+ - Celery docs, [Security: Serializers](https://docs.celeryq.dev/en/stable/userguide/security.html#serializers):
162
+ "*pickle* is inherently insecure, and should be avoided whenever clients are untrusted or unauthenticated"; and, on
163
+ message signing, `setup_security()` "will also disable all insecure serializers so that the worker won't accept
164
+ messages with untrusted content types".
165
+ - Celery docs, [What's new in Celery 4.0](https://docs.celeryq.dev/en/stable/history/whatsnew-4.0.html): JSON became
166
+ the default serializer, and pickle has to be turned back on explicitly with `task_serializer`, `result_serializer`
167
+ and `accept_content`.
168
+ - Celery docs, [Configuration: accept_content](https://docs.celeryq.dev/en/stable/userguide/configuration.html#accept-content):
169
+ the default is `{'json'}`.
170
+ - Steve Dignam, [The Many Problems with Celery](https://steve.dignam.xyz/2023/05/20/many-problems-with-celery):
171
+ pickle among Celery's footguns.
172
+
173
+ ### `celery-practice-countdown-past-visibility-timeout`
174
+
175
+ Rerail's own rule, not a port. A worker holds an eta or countdown task unacknowledged until the task runs. Redis and
176
+ SQS hand an unacknowledged message to another worker once the visibility timeout passes (Redis: one hour by default,
177
+ SQS: 30 minutes), so a task delayed longer runs twice, or "again, and again in a loop". RabbitMQ (3.8.17 and later)
178
+ closes the channel of a consumer that holds a message past its `consumer_timeout` (30 minutes by default), and the
179
+ worker fails with `PreconditionFailed`.
180
+
181
+ It matches, at the delay, a value it can prove is longer than the broker's limit:
182
+
183
+ - `countdown=` and `eta=` on calls resolved to a Celery task, never by method name alone: `.apply_async(...)`,
184
+ `.set(...)` and `.signature(...)` on a task or signature (`lib/celery`'s `cel_task_ref`, which also takes a Celery
185
+ wrapper package's `@task` imported beside celery, as breatheco-de/apiv2's `task_manager`), `.send_task(...)`,
186
+ `celery.signature(...)`, and `.retry(...)` on a bound task's own first parameter (`self.retry`) or on a task;
187
+ - `eta=` the current time plus a timedelta: `datetime.now()`, `utcnow()`, `today()`, Django's `timezone.now()` or
188
+ `timezone.localtime()` (resolved through imports), with `+`/`-` timedeltas in any order and chained;
189
+ - a task's `default_retry_delay=`, when the task retries with it: it calls `.retry()` without a countdown (in its body
190
+ or a function it runs, through the call graph), or sets `autoretry_for` without `retry_backoff`;
191
+ - a task's `retry_backoff_max=`, when it sets `autoretry_for` and `retry_backoff` and its retries reach the cap: the
192
+ backoff before the last retry (`retry_backoff * 2 ** (max_retries - 1)`; `max_retries` from `retry_kwargs`, else the
193
+ option, else Celery's 3; `None` is unbounded) is past the limit.
194
+
195
+ The values it reads: int and float literals, `+` and `*` of values, `timedelta(...)` with readable fields and its
196
+ `.total_seconds()`, `min(a, b)` (its smallest readable argument) and `max(a, b)` (any readable argument), and names,
197
+ resolved by scope (module, enclosing function, own function) through every assignment the name may still hold:
198
+ `TWO_HOURS = 2 * 60 * 60`, a local `later = timezone.now() + timedelta(days=1)` passed as `eta=later`, `step = 600`
199
+ then `if urgent: step = 7200`, a decorator factory's local `max_retries = 60`.
200
+
201
+ The limit follows the run's broker (the schemes of every broker URL literal in its `broker_url` settings and
202
+ `Celery(broker=...)`, the value itself or an environment default; the schemes are Kombu's transport names):
203
+
204
+ - RabbitMQ (`amqp`, `amqps`, `pyamqp`, `librabbitmq` only): the largest `"x-consumer-timeout"` queue argument the run
205
+ sets, else 1800 seconds, RabbitMQ's default `consumer_timeout`. The message says the channel closes, and offers
206
+ raising `consumer_timeout` in `rabbitmq.conf` as the alternative.
207
+ - Otherwise: the largest literal `visibility_timeout` the run sets outside tests, else 1800 seconds on SQS, else 3600
208
+ seconds (Redis, mixed brokers, or a broker it cannot name). The message says the task is redelivered and that a
209
+ longer visibility timeout also delays the recovery of tasks a crashed worker held.
210
+
211
+ It leaves alone: values at or under the limit, and values it cannot read (`countdown=delay` of a parameter,
212
+ `settings.X`, another module's constant, a loop variable, a binding replaced before the end of its scope);
213
+ `.apply_async`, `.set` and `.retry` on receivers that are not Celery tasks (a pool's `apply_async`, a client's
214
+ `retry`, invoke's `@task` in a module without celery); a run that sets the limit (`visibility_timeout`, or
215
+ `x-consumer-timeout` on RabbitMQ) to a value it cannot read, or a non-RabbitMQ run whose `broker_transport_options`
216
+ is not a dict (read from configuration: the team chose a value); test code and test settings files;
217
+ `default_retry_delay` on a task that never retries with it; `retry_backoff_max` on a task whose retries never reach
218
+ it.
219
+
220
+ **Gaps:** a broker chosen by an environment variable with no default is taken for Redis, so a delay between 30 and
221
+ 60 minutes on such a RabbitMQ or SQS run is missed; a `consumer_timeout` raised in `rabbitmq.conf` or a policy,
222
+ outside the repository, is not seen (the message names it); a countdown that grows over retries with no cap
223
+ (`countdown=30 * 2 ** self.request.retries`) is left alone; `options={...}` on signatures, Task subclasses' class
224
+ attributes, `self.apply_async`/`self.retry` in a Task subclass's methods, constants built from other constants
225
+ (`DAY = 24 * HOUR`) and `min()`/`max()` inside arithmetic are not read.
226
+
227
+ **Overlap:** none. No linter reads Celery's delays or broker settings.
228
+
229
+ **Corpus:** 4 matches in 15 Celery repositories, all read, all real (4/4; the review had 2/2 and two misses). Six
230
+ repositories were cloned for this rule (marked *). Runs `celery-final-*` used the final rule on the Junction corpus
231
+ boxes (repositories under 1,000 .py files); `celery-fix-countdown4-*` are the rule writer's runs of the same rework
232
+ before the engine moved off the laptop, on the larger ones.
233
+
234
+ - ecds/readux* (`fcf0d36`, final run): `apps/export/tasks.py:86`, `delete_download_task.apply_async((zipfile_name,),
235
+ countdown=86400)`; no broker and no `visibility_timeout` configured, so the 3600-second limit applies.
236
+ - lookit/lookit-api* (`78562b5`, final run): `studies/models.py:1950`, `countdown=60 * 60 * 24 * 7` with the broker's
237
+ environment default `amqp://`: past RabbitMQ's 1800-second `consumer_timeout`. A review miss, now caught.
238
+ - mozilla/addons-server (`7e3374f`, 1,426 files, writer's run): `src/olympia/abuse/tasks.py:86`,
239
+ `retry_backoff_max=2 * 60 * 60` with `retry_backoff=30` and the decorator factory's `max_retries = 60`, broker
240
+ default `amqp://`. A review miss, now caught.
241
+ - breatheco-de/apiv2* (`2055837`, 2,164 files, not re-run): `breathecode/authenticate/receivers.py:354`,
242
+ `countdown=7200` on a `task_manager` `@task` (a Celery wrapper package imported beside celery, which the library now
243
+ resolves), broker CloudAMQP or Redis by environment. Reported in the review; the case `proj/` reproduces the shape.
244
+ - Silent: makeplane/plane (`723bb17`), zenodo/zenodo-rdm* and paperless-ngx/paperless-ngx (final runs);
245
+ andreynovikov/django-shop* (`visibility_timeout` of 8 days), indico/indico (2 days), langgenius/dify
246
+ (`visibility_timeout` from configuration), UB-Mannheim/escriptorium* (`default_retry_delay=60 * 60`, exactly the
247
+ hour), saleor, DefectDojo and superset (review); pretix/pretix (transport options from its configuration file;
248
+ writer's run of the previous rework).
249
+
250
+ On the review's counterexamples (`celery-final-cx-cv-*`, `celery-final2-cx-cv-*`): 7 findings, all real. The four
251
+ review misses are caught (RabbitMQ `countdown=2 * 60 * 60`, SQS `countdown=45 * 60`, `.s(uid).set(countdown=7200)`,
252
+ `eta=later`), with the three it already caught; `countdown=3000` and `expires=7200` stay silent.
253
+
254
+ **Sources:**
255
+ - Celery docs, [Redis broker caveats](https://docs.celeryq.dev/en/stable/getting-started/backends-and-brokers/redis.html#redis-caveats):
256
+ "If a task isn't acknowledged within the Visibility Timeout the task will be redelivered to another worker and
257
+ executed. This causes problems with ETA/countdown/retry tasks where the time to execute exceeds the visibility
258
+ timeout; in fact if that happens it will be executed again, and again in a loop."
259
+ - Celery docs, [SQS caveats](https://docs.celeryq.dev/en/stable/getting-started/backends-and-brokers/sqs.html#caveats):
260
+ the same for SQS, whose default visibility timeout is 30 minutes.
261
+ - Celery docs, [Calling: ETA and Countdown](https://docs.celeryq.dev/en/stable/userguide/calling.html#eta-and-countdown):
262
+ "tasks are not acknowledged until the worker starts executing them. If using Redis as a broker, task will get
263
+ redelivered when *countdown* exceeds *visibility_timeout*", so eta and countdown are "not recommended for
264
+ scheduling tasks for a distant future. Ideally, use values no longer than several minutes."
265
+ - Adam Johnson, [Common Issues Using Celery (And Other Task Queues)](https://adamj.eu/tech/2020/02/03/common-celery-issues-on-django-projects):
266
+ avoid long etas and countdowns.
267
+ - pretix's own comment, `src/pretix/api/webhooks.py:545`: retries longer than the one-hour `visibility_timeout` "will
268
+ be restarted many times because celery believes the worker has crashed", so pretix stores them in the database
269
+ instead.
270
+
271
+ ### `celery-practice-result-get-in-task`
272
+
273
+ Rerail's own rule, not a port. A task that waits for another task's result holds its worker pool slot until the
274
+ other task finishes. When every slot is held by a waiting task, nothing is left to run the tasks they wait for, and the
275
+ pool deadlocks. Celery guards against it at run time: `AsyncResult.get()`/`wait()` and `ResultSet.join()`/
276
+ `join_native()` call `assert_will_not_block()`, which raises `RuntimeError("Never call result.get() within a task!")`
277
+ in the worker unless the call passes `disable_sync_subtasks=False` or runs inside `with allow_join_result():`. The
278
+ same code runs in a shell, a test with eager tasks, or a web process, so it fails only in the worker, in production.
279
+
280
+ It matches, at the wait, `.get()`, `.join()`, `.join_native()`, `.wait()` or `.collect()` on a result, when the call is
281
+ in a task's body (a `@shared_task`/`@app.task` def, or the `run` method of a class deriving from celery's `Task`) or in
282
+ a function a task reaches through the run's call graph, across files. A wait in a helper names the helper and one task
283
+ that runs it. A result is:
284
+
285
+ - `t.delay(...)`, `t.apply_async(...)` on a Celery task (a name that resolves to a task def, a task object or a
286
+ signature; RQ's `@job` or a thread pool's `apply_async` is not one), `<x>.send_task(...)`;
287
+ - `AsyncResult(id)`, `app.AsyncResult(id)`, `self.AsyncResult(id)`, `celery.result.AsyncResult(id)`, `GroupResult(...)`,
288
+ `ResultSet(...)`, `GroupResult.restore(id)` (also `app.GroupResult.restore`), and the same for a class of the run
289
+ deriving from one of them (pretix's `SafeAsyncResult`);
290
+ - a group, chain or chord sent: `group(...)()`, `group(...).apply_async()`, `chord(header)(body)`, `chain(...)()`,
291
+ `(a.s() | b.s()).apply_async()`, and `.delay()`/`.apply_async()` on a signature (`t.s(...)`, `signature(...)`);
292
+ - a call of a function of the run that returns one of those (pretix's `get_task_result`);
293
+ - a name whose latest plain assignment before the wait is one of those (`r = t.delay(x)` then `r.get()`);
294
+ - an element of a list, tuple, dict or comprehension of results (`pending[i]`), and the variable of a `for` statement
295
+ or a comprehension over one (`results = [t.delay(i) for i in ids]`, then `[r.get() for r in results]`).
296
+
297
+ It leaves alone: a wait that passes `disable_sync_subtasks=` anything but `True` (DefectDojo's
298
+ `dojo/celery_dispatch.py:141` does so deliberately, on an eager result); a wait inside `with allow_join_result():`, and a
299
+ helper a task reaches only through calls made inside such a block; `.get()` on anything not known to be a result
300
+ (dicts, caches, querysets, `options.get(...)`, a name last rebound to something else); status reads (`ready()`,
301
+ `state`, `successful()`, `id`, `completed_count()`); `t.apply(...)`, whose EagerResult never blocks; code no task
302
+ reaches (a view polling a result is fine); tasks and waits in test files. A function reached from both web code and a
303
+ task is reported: the task's path blocks.
304
+
305
+ **Gaps:** results passed in as parameters or kept in attributes; calls the call graph cannot resolve; a project with
306
+ `task_always_eager` on, where the wait does not block, is reported all the same.
307
+
308
+ **Overlap:** none. No linter knows which functions run in a Celery worker.
309
+
310
+ **Corpus:** no match in 20 Celery repositories, before or after the rework, so precision on real code is
311
+ unmeasured and the cases carry the rule. Re-run after the rework: paperless-ngx/paperless-ngx, pretix/pretix,
312
+ mozilla/addons-server, taigaio/taiga-back, Netflix/lemur, flaskbb/flaskbb, zenodo/zenodo-rdm, Buuntu/fastapi-react
313
+ (runs `celery-fix-three-*`) and makeplane/plane (`celery-final-*`): 0. The review's text search of 10 Celery
314
+ repositories found one task-side wait, DefectDojo's `dojo/celery_dispatch.py:141`, which opts out with
315
+ `disable_sync_subtasks=False` and stays silent. On the review's counterexamples (`celery-final-cx-rget`): 5 findings,
316
+ all real: the list and dict fan-outs the review listed as misses (`[r.get() for r in results]`,
317
+ `pending[i].get()`), `AsyncResult(task_id).get()`, a child's `get(timeout=10)` and `get(propagate=False)`; the
318
+ thread pool's `apply_async(...).get()`, the review's false positive, is silent.
319
+
320
+ **Sources:**
321
+ - Celery docs, [Tasks: Avoid launching synchronous subtasks](https://docs.celeryq.dev/en/stable/userguide/tasks.html#avoid-launching-synchronous-subtasks):
322
+ waiting on another task's result in a task "is very inefficient, and may even cause a deadlock if the worker pool is
323
+ exhausted"; make the steps asynchronous with a chain or callbacks instead.
324
+ - Celery source, [`celery/result.py`](https://github.com/celery/celery/blob/main/celery/result.py): `E_WOULDBLOCK =
325
+ "Never call result.get() within a task!"`, `assert_will_not_block()`, `allow_join_result()`, and the
326
+ `disable_sync_subtasks` argument of `get()`/`join()`.
327
+ - Celery API reference, [`celery.result`](https://docs.celeryq.dev/en/stable/reference/celery.result.html):
328
+ `disable_sync_subtasks` — "Disable tasks to wait for sub tasks this is the default configuration. CAUTION do not
329
+ enable this unless you must."
330
+
331
+ ### `celery-practice-task-arg-not-serializable`
332
+
333
+ Rerail's own rule, not a port. Celery puts a task's arguments into the message with JSON by default, and kombu's
334
+ encoder adds only datetime, date, time, Decimal, UUID and bytes (and objects with a `__json__` method). Any other
335
+ object makes the send raise EncodeError. With pickle, a SQLAlchemy model arrives in the worker as a detached snapshot
336
+ that is stale by the time the task runs, and saving it there overwrites newer changes.
337
+
338
+ It matches an argument the task receives (`t.delay(x)`, `t.s(x)`, `t.si(x)`, positional, keyword, or through a
339
+ `**{...}`/`*[...]` spread; `t.apply_async((x,))`, `t.apply_async(args=[x])`, `t.apply_async(kwargs={"k": x})` or
340
+ `kwargs=dict(k=x)`; `app.send_task("n", args=[x])`) where `t` resolves through imports to a Celery task (RQ's
341
+ `@job(...)` functions and thread pools have `.delay`/`.apply_async` too, and are not), and the argument is:
342
+
343
+ - a SQLAlchemy model instance: `M(...)`, `session.get(M, pk)`, `session.scalar(select(M)...)`,
344
+ `M.query...first()`, `session.query(M)...one()`, a name bound to one, a parameter annotated `M`, a value the type
345
+ program proves is a model, Flask-Login's `current_user`, or a list, tuple or dict literal holding one. Message: pass
346
+ the primary key and load the row in the task.
347
+ - a SQLAlchemy session, proven by what binds it: a session class's constructor (SQLAlchemy's, SQLModel's, or a
348
+ subclass), a `sessionmaker`/`async_sessionmaker`/`scoped_session` factory or its call or `.begin()`,
349
+ Flask-SQLAlchemy's `db.session` (`db` resolving to a `SQLAlchemy()` object), a run function that returns or yields
350
+ one (`get_db`), `with ... as s` of one, or a parameter annotated with a session class (also inside `Annotated[...]`
351
+ or through an alias such as `SessionDep`). Where nothing binds it, an unannotated parameter whose name ends in
352
+ `session`, never rebound in its function, counts in a run that imports SQLAlchemy. Message: pass the ids and open a
353
+ session in the task.
354
+ - a set or frozenset: a literal, a comprehension, a `set(...)`/`frozenset(...)` call, a `|`, `&`, `-` or `^` with one
355
+ of those (`tags | {"x"}`), or a name whose last assignment before the send is one. Message: pass a list.
356
+ - an open file: `open(...)`, a name whose last assignment is one, or the target of `with open(...) as f`. Message:
357
+ pass the path or the contents.
358
+ - a lambda, a generator expression, or a function of the run passed by name. Message: pass the data, or a name the
359
+ task maps to the function.
360
+
361
+ It leaves alone: `order.id` and other attributes, primitives, lists, tuples and dicts of plain values, `sorted(s)` and
362
+ `list(s)`, a name reassigned to a list before the send; datetime, Decimal, UUID and bytes (kombu round-trips them
363
+ since 5.3); `.delay` on a plain undecorated function or another library's job; a local named `session` bound to
364
+ something else (a request field, `str(uuid4())`, a dict, a `requests` or `aiohttp` session) and a parameter annotated
365
+ with a non-SQLAlchemy type; `session_id`; files that import django for the model shape (the django pack's
366
+ `django-practice-task-arg-model-instance` owns Django sends); sets and functions under a pickle serializer (a task's
367
+ `serializer="pickle"`, a send's `serializer="pickle"`, or an app's `task_serializer` naming pickle, `signed-pickle`
368
+ too), since pickle carries them; the model shape in a run whose Task subclass (resolved through its bases, or the
369
+ app's `self.Task`) overrides `apply_async` and rewrites `args`/`kwargs` there (indico's `_CelerySAWrapper` sends each
370
+ model's identity key and re-fetches it in the worker); test files. Under pickle the model, session, file, lambda and
371
+ generator shapes are still reported: pickle raises on the last four, and ships a stale snapshot of the first.
372
+
373
+ **Gaps:** values whose kind only a type checker knows (a helper whose return the type program cannot follow);
374
+ instances inside comprehensions or containers built elsewhere; related objects (`order.customer`); dataclass and
375
+ Pydantic instances (left out: kombu honours `__json__`, and projects register their own types); a model class that
376
+ defines `__json__` (it encodes, but the snapshot is still stale, so the message stays right); a project that registers
377
+ `set` with kombu's `register_type`; a serializer chosen in task routes or a Task subclass's `serializer` attribute.
378
+
379
+ **Overlap:** none. Ruff, Bandit and mypy do not look at task arguments; `delay` is untyped without celery-types, and
380
+ celery-types accepts any object.
381
+
382
+ **Corpus:** no match in 20 Celery repositories, before or after the rework, so precision on real code is
383
+ unmeasured; every reported shape is a deterministic EncodeError at the send, except a model under pickle. Re-run after
384
+ the rework: paperless-ngx/paperless-ngx, pretix/pretix, mozilla/addons-server, taigaio/taiga-back, Netflix/lemur,
385
+ flaskbb/flaskbb, zenodo/zenodo-rdm, Buuntu/fastapi-react (runs `celery-fix-three-*`) and makeplane/plane
386
+ (`celery-final-*`): 0. indico's pickled models go through its `_CelerySAWrapper`, which the rule exempts. On the
387
+ review's counterexamples (`celery-final-cx-targ`): 7 findings, all real: a model in a list, Flask-Login's
388
+ `current_user`, a `**{...}` spread, `kwargs=dict(...)`, `tags | {"x"}` (the review's misses), and the two it caught;
389
+ the three strings and dicts named `session` and RQ's `rq_tag.delay(set(...))`, the review's four false positives, are
390
+ silent.
391
+
392
+ **Sources:**
393
+ - Celery docs, [Tasks: State](https://docs.celeryq.dev/en/stable/userguide/tasks.html#state): "Another gotcha is Django model objects. They shouldn't be passed on as arguments to tasks. It's almost always better to re-fetch the object from the database when the task is running instead, as using old data may lead to race conditions."
394
+ - Deni Bertović, [Celery Best Practices](https://denibertovic.com/posts/celery-best-practices/), No. 7: "You shouldn't pass Database objects (for instance your User model) to a background task because the serialized object might contain stale data. What you want to do is feed the task the User id and have the task ask the database for a fresh User object."
395
+ - BaoDev, [flush vs commit](https://www.baodev.studio/blog/sqlalchemy-flush-vs-commit-difference): "pass primitives to your queue anyway, never ORM objects."
396
+ - kombu, [`kombu/utils/json.py`](https://github.com/celery/kombu/blob/main/kombu/utils/json.py): the encoder registers datetime, date, time, Decimal and UUID and handles bytes; any other type, sets included, falls through to `json.JSONEncoder.default`, which raises TypeError.
397
+
398
+ ### `celery-practice-unknown-task-name`
399
+
400
+ Rerail's own rule, not a port. A Celery message carries only the task's name; the worker looks the name up in its
401
+ registry and, for a name no task registered, logs "Received unregistered task of type ... The message has been ignored
402
+ and discarded." A beat entry whose `"task"` has a typo, or names a task that was renamed or moved to another module, is
403
+ sent on schedule and dropped every time: the job silently never runs, and nothing fails at deploy.
404
+
405
+ It ships in `log` mode and starts on (since 2026-11-30). With no match on real code it is under the release gate's
406
+ floor of 5 judged matches in 2 unseen apps, so it ships on the gate's other condition: no false match of it is known
407
+ anywhere. It matches nothing in the 20 corpus repositories or in the benchmark's unseen repositories (0 matches), and
408
+ its 2 findings on the review's counterexamples are both real. Its precision on real code is still unmeasured, so it
409
+ stays in `log`, and the cases and counterexamples are its evidence.
410
+
411
+ It matches, at the naming site, a task name that no task of the run registers, when the run owns the name:
412
+
413
+ - the first argument (or `name=`) of `send_task(...)` and `signature(...)` on a Celery app (resolved: a module-level
414
+ `Celery(...)`, a factory's result, `current_app`, `self.app`; a look-alike class's `send_task` is not one), and
415
+ celery's own `signature(...)`;
416
+ - the `"task"` of a beat entry: a dict (or `dict(...)` call) with `"task"` and `"schedule"`, or one with `"task"` and
417
+ `"options"` whose value reaches the beat setting: the `beat_schedule`/`CELERY_BEAT_SCHEDULE` value, an item assigned
418
+ to it or passed to its `.update(...)`, a module constant it reads (through imports and `**` spreads), or a run
419
+ function called from it or reached from one through the call graph (paperless-ngx's `parse_beat_schedule()`).
420
+
421
+ Every name is read by one value resolver: a string literal, a module constant of the run through imports, re-exports
422
+ and module aliases, a class attribute (`ExportTask.name`), or a local of its own function, each when bound once. A task
423
+ is registered under its `name=`, else `<module>.<function>` (matched also by a dotted tail), whether the decorator is
424
+ applied with `@` or called as a function (`app.task(fn)`, `shared_task(name="ops.rotate")(fn)`), and on any resolved
425
+ app, also one built by a factory (`cel = make_celery()`). A class deriving from celery's `Task` or from `<app>.Task`
426
+ registers its `name` attribute (resolved the same way), else `<module>.<Class>`. The run owns a name when its module
427
+ part is exactly a module of the run, or when an explicit task name of the run shares its first segment (superset's
428
+ `reports.scheduler`). When a task whose function has the name's last segment is registered elsewhere, a second message
429
+ names it as a moved task. Both messages say to check the name against the worker's registered tasks and to leave it
430
+ when another service or an installed package registers it.
431
+
432
+ It leaves alone: Celery's own tasks (`celery.*`); names whose module part is not exactly a module of the run and
433
+ whose first segment no explicit name shares (another service's task, or a library's); a dict with `"task"` and
434
+ `"options"` that never reaches the beat setting (an ML job list); names built at runtime or held by a constant outside
435
+ the run; commented-out entries; test code.
436
+
437
+ **Gaps:** a name computed by a decorator wrapper, or held by an installed package's constant (that task registers
438
+ nothing, so a string naming it is reported if its module is the run's); a run whose module names carry a leading
439
+ package the worker does not (`api.tasks.x` imported as `tasks.x`) no longer owns `tasks.*`; beat entries added at
440
+ run time (`add_periodic_task`, django-celery-beat rows); whether the worker imports the module that defines a task.
441
+
442
+ **Overlap:** none. No linter resolves Celery task names across files; the worker's log reports them at run time.
443
+
444
+ **Corpus:** no match in 20 Celery repositories, before or after the rework, and none was due: the hand check of the
445
+ beat entries and string sends in 8 of them found every name registered. Re-run with the reworked rule:
446
+ paperless-ngx/paperless-ngx, pretix/pretix, mozilla/addons-server, makeplane/plane and apache/superset (rule writer's
447
+ runs `celery-fix-unknown-name-*`), and plane again with the final library (`celery-final-*`): 0. Evidence that it
448
+ fires: on the review's counterexamples (`celery-final-cx-uk`) 2 findings, both real (the typo `app.tasks.local_jbo`
449
+ and the review's missed `dict(task="app.tasks.missing", ...)` item assignment), and the review's four false positives
450
+ (a factory-built app, a class name from a constant, another service's `tasks.x`, an ML job list with `"options"`) are
451
+ silent; a copy of paperless-ngx's settings with one name changed (`documents.tasks.empty_trsh`) reports exactly that
452
+ line through `parse_beat_schedule()`. Precision on real code stays unmeasured, hence log; no false match is known, hence
453
+ on.
454
+
455
+ **Sources:**
456
+ - Celery docs, [Tasks: Names](https://docs.celeryq.dev/en/stable/userguide/tasks.html#names): every task has a unique
457
+ name, generated from the module and function name unless `name=` is given, and the name is what the message carries.
458
+ - Celery source, [worker consumer](https://docs.celeryq.dev/en/stable/_modules/celery/worker/consumer/consumer.html):
459
+ `UNKNOWN_TASK_ERROR`, "Received unregistered task of type %s. The message has been ignored and discarded."
460
+ - Stack Overflow, [Celery "Received unregistered task of type"](https://stackoverflow.com/questions/30746807) and
461
+ [Python Celery Received unregistered task of type - import app error](https://stackoverflow.com/questions/47161489):
462
+ messages dropped because the name sent does not match the name the worker registered.