@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/LICENSE +202 -0
- package/NOTICE +7 -0
- package/README.md +461 -2
- package/pack.json +180 -0
- package/package.json +35 -4
- package/rules/celery-practice-countdown-past-visibility-timeout.rerail +462 -0
- package/rules/celery-practice-dispatch-before-commit.rerail +467 -0
- package/rules/celery-practice-pickle-serializer.rerail +128 -0
- package/rules/celery-practice-result-get-in-task.rerail +210 -0
- package/rules/celery-practice-task-arg-not-serializable.rerail +279 -0
- package/rules/celery-practice-unknown-task-name.rerail +281 -0
- package/rules/lib/celery.rerail +683 -0
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
.policy "celery-practice-dispatch-before-commit" revision 1
|
|
2
|
+
.description "Send Celery tasks after the SQLAlchemy session commits, not between a write and its commit, so the worker never reads rows that are not committed yet."
|
|
3
|
+
.language python
|
|
4
|
+
.mode steer
|
|
5
|
+
.scope include "**/*.py"
|
|
6
|
+
// Rerail's own rule, not a port: the SQLAlchemy side of django-practice-enqueue-on-commit. A task sent while the
|
|
7
|
+
// session holds uncommitted writes can start on a worker before the commit: it finds no row or the old one, or acts
|
|
8
|
+
// on a change that then rolls back.
|
|
9
|
+
// What it resolves rather than spells:
|
|
10
|
+
// - a send is lib/celery's cel_send: `.delay()`/`.apply_async()` on a resolved Celery task (a task def of the run
|
|
11
|
+
// through imports, aliases and re-exports, a task object, a Task instance, a signature `t.s(...)`) or
|
|
12
|
+
// `<app>.send_task()`; or a call of a run function that sends, through the call graph, with no commit of its
|
|
13
|
+
// own on the way (reported at that call, naming the helper). RQ's `@job`, a thread pool or any class with a
|
|
14
|
+
// `delay` method is not a task;
|
|
15
|
+
// - a session is lib/celery's sa_session: proven through its binding (Session/sessionmaker/scoped_session,
|
|
16
|
+
// Flask-SQLAlchemy's db.session, an annotation, `with ... as`, a run function returning one), with a name
|
|
17
|
+
// fallback only where the binding resolves to nothing;
|
|
18
|
+
// - a write is a session call (add, add_all, delete, merge, flush, bulk_*, execute of insert/update/delete), or an
|
|
19
|
+
// assignment to a mapped column (resolved: mapped_column/Column/relationship/Mapped/SQLModel fields, inherited
|
|
20
|
+
// too) of a row the function loaded through that session (`get`, `query(M)...first()`, `scalar(select(M))`,
|
|
21
|
+
// `execute(...).scalar_one()`, or a `for` over its rows), which the unit of work flushes at commit;
|
|
22
|
+
// - a path is statement order with the pairs no path runs taken out: different arms of one if/elif/else, match, or
|
|
23
|
+
// try's except/else; a write whose block returns (or raises outside a try) before the send.
|
|
24
|
+
// Reported when a write reaches the send and then
|
|
25
|
+
// 1. a commit of the same session follows in the function, with no commit of it on the way from write to send;
|
|
26
|
+
// 2. the send sits in the body of `with <x>.begin():`/`async with`, whose transaction holds the write;
|
|
27
|
+
// 3. the function never commits, and a caller (through callers that do not commit either) commits the same
|
|
28
|
+
// session after the call or makes it inside a begin block on it; the session is followed through each call,
|
|
29
|
+
// parameter to argument (dify's _update_internal_dataset → update_dataset → the adapter's `with` block);
|
|
30
|
+
// 4. or, with no write needed, the send runs in a flush-time event listener (`@event.listens_for(X,
|
|
31
|
+
// "after_insert")`, `event.listen(X, "before_flush", fn)`, the other mapper and flush events, before_commit).
|
|
32
|
+
// session.add(order); session.flush()
|
|
33
|
+
// ship.delay(order.id) reported
|
|
34
|
+
// session.commit()
|
|
35
|
+
// ship.delay(order.id) not reported: after the commit
|
|
36
|
+
// if is_new: session.add(order)
|
|
37
|
+
// else: ship.delay(order.id) not reported: the send's path writes nothing
|
|
38
|
+
// event.listen(session, "after_commit", lambda s: ship.delay(order.id)) not reported: runs after the commit
|
|
39
|
+
// Also left alone: a send in an except handler that always re-raises (the transaction rolls back), `r =
|
|
40
|
+
// t.apply_async(...)` whose result the function reads afterwards (the task id is stored on the row, so the send cannot
|
|
41
|
+
// simply move after the commit), a send in a lambda or nested def, a caller committing another session, test files,
|
|
42
|
+
// and documents that import django (the django pack's dispatch rules own them).
|
|
43
|
+
// Still by spelling: within one function, writes and commits pair by the session's text (`session`, `self.session`),
|
|
44
|
+
// which is one variable in practice; across calls it is carried through parameters, not by name.
|
|
45
|
+
// Gaps: loops (a write later in the loop body precedes the next iteration's send); writes through repository objects
|
|
46
|
+
// or a row loaded elsewhere and passed in; a Flask `Model.query.get()` row (no session spelled); a transaction a
|
|
47
|
+
// framework opens around the request; a commit in only some arms between the write and the send counts as a commit
|
|
48
|
+
// between them.
|
|
49
|
+
|
|
50
|
+
#include "lib/celery.rerail"
|
|
51
|
+
#include "python/flow.rerail"
|
|
52
|
+
|
|
53
|
+
.violation DispatchBeforeCommit(at: node, what: symbol, where: symbol) "{what} runs {where}: the worker can start before the commit and miss the row. Send it after `commit()` or from an `after_commit` event; leave it if the task reads nothing written here."
|
|
54
|
+
|
|
55
|
+
// Each function's writes, commits and sends, and their order, are stated in its own file, with what each needs of
|
|
56
|
+
// other files (needs, as lib/celery's: a task def the send names, a session factory the write's session comes from, a
|
|
57
|
+
// mapped column "dbc_col:<x>" of the row's model, a run function that sends "dbc_sends"; dbc_holds answers them).
|
|
58
|
+
// What crosses functions is by symbol (decision 0677: no node crosses files): a def by its symbol (`pt_def`, as
|
|
59
|
+
// python/calls' `call_target` names one), a call, a send or a begin block by its term (`pt_tm`), sessions by their
|
|
60
|
+
// spelling. Cases 1, 2 and 4 are a function's own (one file); case 3 follows the calls (dbc_up_q, dbc_cc_q).
|
|
61
|
+
// 1. write, send, commit in one function.
|
|
62
|
+
DispatchBeforeCommit(at, w, "between a session write and this function's `commit()`") :-
|
|
63
|
+
dbc_send_at(at, f, w, sq, sk), dbc_holds(sq, sk), dbc_write(x, f, s, nq, nk, mq, mk), dbc_holds(nq, nk),
|
|
64
|
+
dbc_holds(mq, mk), earlier(x, at), !dbc_unreach(x, at), dbc_commit(k, f, s, kq, kk), cel_is(kq, kk), earlier(at, k),
|
|
65
|
+
!dbc_apart(k, at), !dbc_committed_between(x, at, f, s), dbc_reported(at).
|
|
66
|
+
// 2. in a with-begin body after a write: of the block's own session, or of any session when it opens one the engine
|
|
67
|
+
// cannot name (no candidate of dbc_tx_session holds: dbc_tx_known_t, by the block's term).
|
|
68
|
+
DispatchBeforeCommit(at, w, "after a write inside `with ….begin()`, which commits at its end") :-
|
|
69
|
+
dbc_send_at(at, f, w, sq, sk), dbc_holds(sq, sk), dbc_in_tx(at, b), dbc_write(x, f, s, nq, nk, mq, mk),
|
|
70
|
+
dbc_holds(nq, nk), dbc_holds(mq, mk), dbc_in_tx(x, b), dbc_tx_session(b, s, tq, tk), cel_is(tq, tk), earlier(x, at),
|
|
71
|
+
!dbc_unreach(x, at), dbc_reported(at).
|
|
72
|
+
DispatchBeforeCommit(at, w, "after a write inside `with ….begin()`, which commits at its end") :-
|
|
73
|
+
dbc_send_at(at, f, w, sq, sk), dbc_holds(sq, sk), dbc_in_tx(at, b), dbc_write(x, f, _, nq, nk, mq, mk),
|
|
74
|
+
dbc_holds(nq, nk), dbc_holds(mq, mk), dbc_in_tx(x, b), pt_tm(b, bt), !dbc_tx_known_t(bt), earlier(x, at),
|
|
75
|
+
!dbc_unreach(x, at), dbc_reported(at).
|
|
76
|
+
// 3. the caller commits (dbc_cc_q, by the send's term).
|
|
77
|
+
DispatchBeforeCommit(at, w, "after a session write its caller commits") :- dbc_send_at(at, _, w, sq, sk),
|
|
78
|
+
dbc_holds(sq, sk), pt_tm(at, att), dbc_cc_q(att), dbc_reported(at).
|
|
79
|
+
// 4. a flush-time event listener, registered by its decorator (dbc_listener) or by `event.listen(...)` in any file
|
|
80
|
+
// (dbc_listener_q, by its symbol). One report per send, however many events the listener is registered for.
|
|
81
|
+
DispatchBeforeCommit(at, w, "in a flush-time SQLAlchemy event listener") :- dbc_listener(f), dbc_send_at(at, f, w, sq, sk),
|
|
82
|
+
dbc_holds(sq, sk), dbc_reported(at).
|
|
83
|
+
DispatchBeforeCommit(at, w, "in a flush-time SQLAlchemy event listener") :- dbc_send_at(at, f, w, sq, sk),
|
|
84
|
+
dbc_holds(sq, sk), qualname(f, fq), dbc_listener_q(fq), dbc_reported(at).
|
|
85
|
+
.decl dbc_reported(at: node)
|
|
86
|
+
dbc_reported(at) :- docof(at, d), !cel_test_doc(d), !cel_django_doc(d).
|
|
87
|
+
|
|
88
|
+
// ---- needs -------------------------------------------------------------------------------------------------------
|
|
89
|
+
// dbc_holds(q, k): lib/celery's cel_is, and this rule's kinds over symbols: "dbc_col:<x>" (model class q maps
|
|
90
|
+
// attribute x to a column: dbc_colq), "dbc_sends" (run function q sends a task when called, and is no task itself:
|
|
91
|
+
// dbc_sends_q). It spans files on purpose (a model's columns are declared in its bases' modules, a helper that
|
|
92
|
+
// sends in another module); symbols alone: cross_file.
|
|
93
|
+
.decl dbc_holds(q: symbol, k: symbol) cross_file
|
|
94
|
+
dbc_holds(q, k) :- cel_is(q, k).
|
|
95
|
+
dbc_holds(q, k) :- dbc_colq(q, x), cel_is(q, "sa_model"), k = cat("dbc_col:", x).
|
|
96
|
+
dbc_holds(gq, "dbc_sends") :- dbc_sends_q(gq), !cel_is(gq, "task").
|
|
97
|
+
|
|
98
|
+
// ---- regions ---------------------------------------------------------------------------------------------------
|
|
99
|
+
// dbc_own(n, f): n runs in def f's own body: no nested def, lambda or class between (a lambda runs later).
|
|
100
|
+
.decl dbc_own(n: node, f: node)
|
|
101
|
+
dbc_own(n, f) :- kind(f, "function_definition"), field(f, "body", b), within_stop(n, b, cel_region_stop).
|
|
102
|
+
// dbc_in_tx(n, b): n runs in the body b of a `with <x>.begin():`, in the same def.
|
|
103
|
+
.decl dbc_in_tx(n: node, b: node)
|
|
104
|
+
dbc_in_tx(n, b) :- sa_tx_with(_, b), within_stop(n, b, cel_region_stop).
|
|
105
|
+
// dbc_tx_session(b, s, nq, nk): the begin block with body b runs on the session spelled s: the `as` name of `with
|
|
106
|
+
// <maker>.begin() as s:`, or the receiver of `with s.begin():`, when it is a session.
|
|
107
|
+
.decl dbc_tx_item(b: node, v: node)
|
|
108
|
+
dbc_tx_item(b, v) :- sa_tx_with(w, b), child(w, wc, _), kind(wc, "with_clause"), child(wc, wi, _),
|
|
109
|
+
kind(wi, "with_item"), field(wi, "value", v), sa_begin_value(v).
|
|
110
|
+
.decl dbc_tx_session(b: node, s: symbol, nq: symbol, nk: symbol)
|
|
111
|
+
dbc_tx_session(b, s, "", "") :- dbc_tx_item(b, v), kind(v, "as_pattern"), field(v, "alias", al), text(al, s).
|
|
112
|
+
dbc_tx_session(b, s, nq, nk) :- dbc_tx_item(b, v), (v0 = v ; (kind(v, "as_pattern"), child(v, v0, 0))),
|
|
113
|
+
unparen(v0, k), kind(k, "call"), field(k, "function", fn), attr_of(fn, o, "begin"), unparen(o, u),
|
|
114
|
+
sa_session(u, nq, nk), text(u, s).
|
|
115
|
+
// dbc_tx_known_t(b): the block whose body has term b runs on a session the engine names. A fact over terms, as each
|
|
116
|
+
// file states its blocks' candidates (dbc_tx_cand_t), their needs met.
|
|
117
|
+
.decl dbc_tx_known_t(b: symbol)
|
|
118
|
+
dbc_tx_known_t(bt) :- dbc_tx_cand_t(bt, nq, nk), cel_is(nq, nk).
|
|
119
|
+
.decl dbc_tx_cand_t(b: symbol, nq: symbol, nk: symbol)
|
|
120
|
+
dbc_tx_cand_t(bt, nq, nk) :- dbc_tx_session(b, _, nq, nk), pt_tm(b, bt).
|
|
121
|
+
// dbc_sname(s): s spells a session somewhere in the run (sa_session's candidates, their needs met).
|
|
122
|
+
.decl dbc_sname(s: symbol)
|
|
123
|
+
dbc_sname(s) :- dbc_sname_t(s, nq, nk), cel_is(nq, nk).
|
|
124
|
+
.decl dbc_sname_t(s: symbol, nq: symbol, nk: symbol)
|
|
125
|
+
dbc_sname_t(s, nq, nk) :- sa_session(e, nq, nk), text(e, s).
|
|
126
|
+
|
|
127
|
+
// ---- writes ------------------------------------------------------------------------------------------------------
|
|
128
|
+
// A write is what the session sends at its next flush: a session call (sa_write: add, delete, merge, flush, bulk_*,
|
|
129
|
+
// execute of insert/update/delete), or an assignment to a mapped column of a row this function loaded through the
|
|
130
|
+
// session, which the unit of work flushes at commit (`order = session.get(Order, 1); order.status = "paid"`).
|
|
131
|
+
// dbc_colq(q, x): x is a mapped attribute of class q or of a class it derives from, resolved through imports: assigned
|
|
132
|
+
// SQLAlchemy's `mapped_column(...)`, `Column(...)`, `relationship(...)` (or a Flask-SQLAlchemy `db.Column(...)`),
|
|
133
|
+
// SQLModel's `Field(...)`/`Relationship(...)`, annotated SQLAlchemy's `Mapped[...]`, or any annotated field of a
|
|
134
|
+
// SQLModel `table=True` class. Each class's own columns and bases are stated in its file (dbc_col_here,
|
|
135
|
+
// dbc_col_base); the closure over the run's classes is by symbol (cross_file).
|
|
136
|
+
.decl dbc_col_fn_q(q: symbol)
|
|
137
|
+
dbc_col_fn_q("sqlalchemy.orm.mapped_column"). dbc_col_fn_q("sqlalchemy.orm.relationship").
|
|
138
|
+
dbc_col_fn_q("sqlalchemy.orm.column_property"). dbc_col_fn_q("sqlalchemy.orm.composite").
|
|
139
|
+
dbc_col_fn_q("sqlalchemy.Column"). dbc_col_fn_q("sqlalchemy.schema.Column"). dbc_col_fn_q("sqlalchemy.sql.schema.Column").
|
|
140
|
+
dbc_col_fn_q("sqlmodel.Field"). dbc_col_fn_q("sqlmodel.Relationship").
|
|
141
|
+
.decl dbc_col_fn_flask(m: symbol)
|
|
142
|
+
dbc_col_fn_flask("Column"). dbc_col_fn_flask("relationship"). dbc_col_fn_flask("column_property").
|
|
143
|
+
.decl dbc_colq(q: symbol, x: symbol) cross_file
|
|
144
|
+
dbc_colq(q, x) :- dbc_col_here(q, x, nq, nk), cel_is(nq, nk).
|
|
145
|
+
dbc_colq(q, x) :- dbc_col_base(q, b), dbc_colq(b, x).
|
|
146
|
+
.decl dbc_col_here(q: symbol, x: symbol, nq: symbol, nk: symbol)
|
|
147
|
+
dbc_col_here(q, x, nq, nk) :- dbc_class_field(c, x, a), class_def(c, q), dbc_col_value(a, nq, nk).
|
|
148
|
+
dbc_col_here(q, x, "", "") :- dbc_class_field(c, x, a), class_def(c, q), field(a, "type", _),
|
|
149
|
+
class_base(c, "sqlmodel.SQLModel"), sa_table_kw(c).
|
|
150
|
+
.decl dbc_col_base(q: symbol, b: symbol)
|
|
151
|
+
dbc_col_base(q, b) :- class_def(c, q), cel_class_base(c, b).
|
|
152
|
+
// dbc_class_field(c, x, a): assignment a (annotated or not) in class c's body binds x.
|
|
153
|
+
.decl dbc_class_field(c: node, x: symbol, a: node)
|
|
154
|
+
dbc_class_field(c, x, a) :- kind(c, "class_definition"), field(c, "body", bd), child(bd, st, _),
|
|
155
|
+
kind(st, "expression_statement"), child(st, a, 0), kind(a, "assignment"), field(a, "left", l),
|
|
156
|
+
kind(l, "identifier"), text(l, x).
|
|
157
|
+
.decl dbc_col_value(a: node, nq: symbol, nk: symbol)
|
|
158
|
+
dbc_col_value(a, "", "") :- field(a, "right", r), unparen(r, k), kind(k, "call"), field(k, "function", fn),
|
|
159
|
+
sym_of(fn, q), dbc_col_fn_q(q).
|
|
160
|
+
dbc_col_value(a, dq, "sa_flask_db") :- field(a, "right", r), unparen(r, k), kind(k, "call"), field(k, "function", fn),
|
|
161
|
+
attr_of(fn, o, m), dbc_col_fn_flask(m), unparen(o, u), cel_sym(u, dq).
|
|
162
|
+
dbc_col_value(a, "", "") :- field(a, "type", t), (m = t ; within(m, t)), sym_of(m, "sqlalchemy.orm.Mapped").
|
|
163
|
+
// dbc_select_q(e, q): `select(M)` and chains of it (`.where(...)`), M naming class q (a model: its "dbc_col:<x>"
|
|
164
|
+
// need asks).
|
|
165
|
+
.decl dbc_select_q(e: node, q: symbol)
|
|
166
|
+
dbc_select_q(e, q) :- unparen(e, k), kind(k, "call"), field(k, "function", fn), sym_of(fn, fq),
|
|
167
|
+
(fq = "sqlalchemy.select" ; fq = "sqlmodel.select" ; fq = "sqlalchemy.sql.expression.select" ;
|
|
168
|
+
fq = "sqlalchemy.future.select"), posarg(k, 0, r), !posarg(k, 1, _), cel_sym(r, q).
|
|
169
|
+
dbc_select_q(e, q) :- unparen(e, k), kind(k, "call"), field(k, "function", fn), attr_of(fn, o, m), sa_query_method(m),
|
|
170
|
+
dbc_select_q(o, q).
|
|
171
|
+
// dbc_query_q(e, s, q, nq, nk): `s.query(M)` on the session spelled s, and chains of it.
|
|
172
|
+
.decl dbc_query_q(e: node, s: symbol, q: symbol, nq: symbol, nk: symbol)
|
|
173
|
+
dbc_query_q(e, s, q, nq, nk) :- unparen(e, k), sa_session_call(k, o, "query", nq, nk), text(o, s), posarg(k, 0, r),
|
|
174
|
+
!posarg(k, 1, _), cel_sym(r, q).
|
|
175
|
+
dbc_query_q(e, s, q, nq, nk) :- unparen(e, k), kind(k, "call"), field(k, "function", fn), attr_of(fn, o, m),
|
|
176
|
+
sa_query_method(m), dbc_query_q(o, s, q, nq, nk).
|
|
177
|
+
// dbc_row(v, s, q, nq, nk): v loads one row of class q through the session spelled s.
|
|
178
|
+
.decl dbc_row(v: node, s: symbol, q: symbol, nq: symbol, nk: symbol)
|
|
179
|
+
dbc_row(v, s, q, nq, nk) :- unparen(v, k), kind(k, "await"), child(k, c, 0), dbc_row(c, s, q, nq, nk).
|
|
180
|
+
dbc_row(v, s, q, nq, nk) :- unparen(v, k), sa_session_call(k, o, m, nq, nk), (m = "get" ; m = "get_one"), text(o, s),
|
|
181
|
+
posarg(k, 0, r), cel_sym(r, q).
|
|
182
|
+
dbc_row(v, s, q, nq, nk) :- unparen(v, k), kind(k, "call"), field(k, "function", fn), attr_of(fn, o, m),
|
|
183
|
+
sa_row_method(m), dbc_query_q(o, s, q, nq, nk).
|
|
184
|
+
dbc_row(v, s, q, nq, nk) :- unparen(v, k), sa_session_call(k, o, "scalar", nq, nk), text(o, s), posarg(k, 0, sel),
|
|
185
|
+
dbc_select_q(sel, q).
|
|
186
|
+
dbc_row(v, s, q, nq, nk) :- unparen(v, k), kind(k, "call"), field(k, "function", fn), attr_of(fn, x0, m),
|
|
187
|
+
sa_scalar_row_method(m), unparen(x0, x), dbc_rows_of(x, s, q, nq, nk).
|
|
188
|
+
// dbc_rows_of(e, s, q, nq, nk): e yields rows of q through s: `s.query(M)...` (iterated, or `.all()`),
|
|
189
|
+
// `s.scalars(select(M))`, `s.execute(select(M)).scalars()`, and `.all()` of those.
|
|
190
|
+
.decl dbc_rows_of(e: node, s: symbol, q: symbol, nq: symbol, nk: symbol)
|
|
191
|
+
dbc_rows_of(e, s, q, nq, nk) :- dbc_query_q(e, s, q, nq, nk).
|
|
192
|
+
dbc_rows_of(e, s, q, nq, nk) :- unparen(e, k), sa_session_call(k, o, m, nq, nk), (m = "scalars" ; m = "execute"),
|
|
193
|
+
text(o, s), posarg(k, 0, sel), dbc_select_q(sel, q).
|
|
194
|
+
dbc_rows_of(e, s, q, nq, nk) :- unparen(e, k), kind(k, "await"), child(k, c, 0), dbc_rows_of(c, s, q, nq, nk).
|
|
195
|
+
dbc_rows_of(e, s, q, nq, nk) :- unparen(e, k), kind(k, "call"), field(k, "function", fn), attr_of(fn, o, m),
|
|
196
|
+
(m = "all" ; m = "scalars" ; m = "unique"), dbc_rows_of(o, s, q, nq, nk).
|
|
197
|
+
// dbc_bound_row(f, x, s, q, nq, nk): name x in f holds a row of q loaded through s: `x = <row>`, or `for x in
|
|
198
|
+
// <rows>:`.
|
|
199
|
+
.decl dbc_bound_row(f: node, x: symbol, s: symbol, q: symbol, nq: symbol, nk: symbol)
|
|
200
|
+
dbc_bound_row(f, x, s, q, nq, nk) :- asg(f, x, v, _), kind(f, "function_definition"), dbc_row(v, s, q, nq, nk).
|
|
201
|
+
dbc_bound_row(f, x, s, q, nq, nk) :- kind(fs, "for_statement"), field(fs, "left", lt), kind(lt, "identifier"),
|
|
202
|
+
text(lt, x), field(fs, "right", rt), dbc_rows_of(rt, s, q, nq, nk), dbc_own(fs, f).
|
|
203
|
+
// dbc_write(w, f, s, nq, nk, mq, mk): write w in f's own body, on the session spelled s; an assignment to a row's
|
|
204
|
+
// attribute (dbc_dirty) needs that attribute to be a mapped column of the row's model ((q, "dbc_col:<x>")).
|
|
205
|
+
// dbc_commit(k, f, s, nq, nk) likewise. dbc_write_c, dbc_commit_c: the candidates, whatever they need, where only
|
|
206
|
+
// their places are read (the paths below, and a commit of a session a write already spelled).
|
|
207
|
+
.decl dbc_write(w: node, f: node, s: symbol, nq: symbol, nk: symbol, mq: symbol, mk: symbol)
|
|
208
|
+
dbc_write(w, f, s, nq, nk, "", "") :- sa_write(w, nq, nk), dbc_own(w, f), sa_session_call(w, o, _, nq, nk), text(o, s).
|
|
209
|
+
dbc_write(a, f, s, nq, nk, q, ck) :- (kind(a, "assignment") ; kind(a, "augmented_assignment")), field(a, "left", l),
|
|
210
|
+
attr_of(l, o, c), unparen(o, u), kind(u, "identifier"), text(u, x), dbc_own(a, f), dbc_bound_row(f, x, s, q, nq, nk),
|
|
211
|
+
ck = cat("dbc_col:", c).
|
|
212
|
+
.decl dbc_write_c(w: node, f: node, s: symbol)
|
|
213
|
+
dbc_write_c(w, f, s) :- dbc_write(w, f, s, _, _, _, _).
|
|
214
|
+
.decl dbc_commit(k: node, f: node, s: symbol, nq: symbol, nk: symbol)
|
|
215
|
+
dbc_commit(k, f, s, nq, nk) :- sa_commit(k, nq, nk), dbc_own(k, f), sa_session_call(k, o, _, nq, nk), text(o, s).
|
|
216
|
+
.decl dbc_commit_c(k: node, f: node, s: symbol)
|
|
217
|
+
dbc_commit_c(k, f, s) :- dbc_commit(k, f, s, _, _).
|
|
218
|
+
// dbc_commits_q(f): def f (by symbol) commits some session. A fact over symbols, from each file's commits
|
|
219
|
+
// (dbc_commit_need), their needs met.
|
|
220
|
+
.decl dbc_commits_q(f: symbol)
|
|
221
|
+
dbc_commits_q(fq) :- dbc_commit_need(fq, nq, nk), cel_is(nq, nk).
|
|
222
|
+
.decl dbc_commit_need(f: symbol, nq: symbol, nk: symbol)
|
|
223
|
+
dbc_commit_need(fq, nq, nk) :- dbc_commit(_, f, _, nq, nk), pt_def(f, fq).
|
|
224
|
+
|
|
225
|
+
// ---- paths -----------------------------------------------------------------------------------------------------
|
|
226
|
+
// Statement order (`earlier`) is the base; these take out pairs no path runs in that order.
|
|
227
|
+
// dbc_node(n): a call or a write in the own body of a function that writes or commits.
|
|
228
|
+
.decl dbc_db_fn(f: node)
|
|
229
|
+
dbc_db_fn(f) :- dbc_write_c(_, f, _).
|
|
230
|
+
dbc_db_fn(f) :- dbc_commit_c(_, f, _).
|
|
231
|
+
.decl dbc_node(n: node)
|
|
232
|
+
dbc_node(n) :- dbc_db_fn(f), dbc_own(n, f), kind(n, "call").
|
|
233
|
+
dbc_node(n) :- dbc_write_c(n, _, _).
|
|
234
|
+
// dbc_alt(st, a): a is an arm of statement st, and arms never run together: an if's body and each of its elif and
|
|
235
|
+
// else clauses, the case clauses of one match, a try's except clauses and its else clause. A try's own body and its
|
|
236
|
+
// finally are not arms: a handler or the finally can run after the body.
|
|
237
|
+
.decl dbc_alt(st: node, a: node)
|
|
238
|
+
dbc_alt(st, a) :- kind(st, "if_statement"), (field(st, "consequence", a) ; field(st, "alternative", a)).
|
|
239
|
+
dbc_alt(st, a) :- kind(a, "case_clause"), child(st, a, _).
|
|
240
|
+
dbc_alt(st, a) :- kind(st, "try_statement"), child(st, a, _), (kind(a, "except_clause") ; kind(a, "else_clause")).
|
|
241
|
+
.decl dbc_arm(n: node, st: node, a: node)
|
|
242
|
+
dbc_arm(n, st, a) :- dbc_node(n), within(n, a), dbc_alt(st, a).
|
|
243
|
+
// dbc_apart(x, n): write or commit x and n sit in different arms of one statement, so no path runs both.
|
|
244
|
+
.decl dbc_apart(x: node, n: node)
|
|
245
|
+
dbc_apart(x, n) :- (dbc_write_c(x, _, _) ; dbc_commit_c(x, _, _)), dbc_arm(x, st, a), dbc_arm(n, st, b), a != b.
|
|
246
|
+
// dbc_exits(x, n): after write or commit x, its block returns (or raises, outside any try) before reaching n, which
|
|
247
|
+
// lies outside that block: `if new: session.add(o); return o` then a send.
|
|
248
|
+
.decl dbc_exits(x: node, n: node)
|
|
249
|
+
dbc_exits(x, n) :- (dbc_write_c(x, f, _) ; dbc_commit_c(x, f, _)), stmt_of(x, sx, bl), child(bl, sx, i), child(bl, r, j),
|
|
250
|
+
j > i, dbc_exit_stmt(r), dbc_node(n), dbc_own(n, f), !within(n, bl).
|
|
251
|
+
.decl dbc_exit_stmt(r: node)
|
|
252
|
+
dbc_exit_stmt(r) :- kind(r, "return_statement").
|
|
253
|
+
dbc_exit_stmt(r) :- kind(r, "raise_statement"), !dbc_in_try_body(r).
|
|
254
|
+
.decl dbc_in_try_body(r: node)
|
|
255
|
+
dbc_in_try_body(r) :- kind(r, "raise_statement"), kind(t, "try_statement"), field(t, "body", tb), within(r, tb).
|
|
256
|
+
// dbc_unreach(x, n): no path runs write or commit x and then n.
|
|
257
|
+
.decl dbc_unreach(x: node, n: node)
|
|
258
|
+
dbc_unreach(x, n) :- dbc_apart(x, n).
|
|
259
|
+
dbc_unreach(x, n) :- dbc_exits(x, n).
|
|
260
|
+
// A commit between them counts only when it runs on the way from the write to the send. It pairs with the write by
|
|
261
|
+
// the session's spelling, which the write's session already is.
|
|
262
|
+
.decl dbc_committed_between(w: node, at: node, f: node, s: symbol)
|
|
263
|
+
dbc_committed_between(w, at, f, s) :- dbc_write_c(w, f, s), dbc_send_at_c(at, f), dbc_commit_c(k, f, s), earlier(w, k),
|
|
264
|
+
earlier(k, at), !dbc_apart(k, w), !dbc_unreach(k, at).
|
|
265
|
+
|
|
266
|
+
// ---- sends -----------------------------------------------------------------------------------------------------
|
|
267
|
+
// dbc_send(s, nq, nk): a send that could move after the commit: not in an except handler that always re-raises, and
|
|
268
|
+
// not `r = t.apply_async(...)` whose r the function reads afterwards.
|
|
269
|
+
.decl dbc_send(s: node, nq: symbol, nk: symbol)
|
|
270
|
+
dbc_send(s, nq, nk) :- cel_send(s, _, nq, nk), !dbc_failing_handler(s), !dbc_result_used(s).
|
|
271
|
+
.decl dbc_failing_handler(s: node)
|
|
272
|
+
dbc_failing_handler(s) :- cel_send(s, _, _, _), kind(x, "except_clause"), within(s, x), child(x, hb, _),
|
|
273
|
+
kind(hb, "block"), always_raises(hb).
|
|
274
|
+
.decl dbc_result_used(s: node)
|
|
275
|
+
dbc_result_used(s) :- cel_send(s, _, _, _), kind(a, "assignment"), field(a, "right", r), unparen(r, s),
|
|
276
|
+
field(a, "left", l), kind(l, "identifier"), text(l, v), def_frame(s, fr), kind(i, "identifier"), text(i, v),
|
|
277
|
+
def_frame(i, fr), i != l, earlier(l, i), !bind_only(i).
|
|
278
|
+
// dbc_send_at(at, f, what, nq, nk): `at` in f's own body sends: a send, or a call of a run function that sends (by
|
|
279
|
+
// name; its need: (g, "dbc_sends")). dbc_send_at_c: the candidates, whatever they need.
|
|
280
|
+
.decl dbc_send_at(at: node, f: node, what: symbol, nq: symbol, nk: symbol)
|
|
281
|
+
dbc_send_at(s, f, cat("`", cat(x, "()`")), nq, nk) :- dbc_send(s, nq, nk), dbc_own(s, f), field(s, "function", e),
|
|
282
|
+
text(e, x).
|
|
283
|
+
dbc_send_at(c, f, cat("`", cat(x, "()` (it sends a task)")), gq, "dbc_sends") :- call_target(c, gq), dbc_own(c, f),
|
|
284
|
+
split_dot(gq, _, x).
|
|
285
|
+
.decl dbc_send_at_c(at: node, f: node)
|
|
286
|
+
dbc_send_at_c(at, f) :- dbc_send_at(at, f, _, _, _).
|
|
287
|
+
// dbc_sends_q(g): calling g (by symbol) sends a task: g's own body sends, or calls a function that does, with no
|
|
288
|
+
// commit of g's on the way (that commit would have committed the caller's writes first). Each file states its
|
|
289
|
+
// functions' sends (dbc_send_own_t) and the commits before them (dbc_cb_cand_t), by the send's term; the walk over
|
|
290
|
+
// the call graph is by symbol (cross_file).
|
|
291
|
+
.decl dbc_sends_q(g: symbol) cross_file
|
|
292
|
+
dbc_sends_q(gq) :- dbc_send_own_t(gq, nt, nq, nk), nk != "dbc_sends", cel_is(nq, nk), !dbc_cb_t(nt).
|
|
293
|
+
dbc_sends_q(gq) :- dbc_send_own_t(gq, nt, hq, "dbc_sends"), dbc_sends_q(hq), !dbc_cb_t(nt).
|
|
294
|
+
.decl dbc_send_own_t(g: symbol, n: symbol, nq: symbol, nk: symbol)
|
|
295
|
+
dbc_send_own_t(gq, nt, nq, nk) :- dbc_send_at(n, g, _, nq, nk), pt_def(g, gq), pt_tm(n, nt).
|
|
296
|
+
// dbc_cb_t(n): a commit of the function runs on the way to the send or call with term n.
|
|
297
|
+
.decl dbc_cb_t(n: symbol)
|
|
298
|
+
dbc_cb_t(nt) :- dbc_cb_cand_t(nt, kq, kk), cel_is(kq, kk).
|
|
299
|
+
.decl dbc_cb_cand_t(n: symbol, kq: symbol, kk: symbol)
|
|
300
|
+
dbc_cb_cand_t(nt, kq, kk) :- dbc_commit(k, f, _, kq, kk), dbc_send_at_c(n, f), earlier(k, n), !dbc_unreach(k, n),
|
|
301
|
+
pt_tm(n, nt).
|
|
302
|
+
|
|
303
|
+
// ---- 3. the caller commits ---------------------------------------------------------------------------------------
|
|
304
|
+
// The session is followed through each call: a callee's session parameter is the caller's argument for it; a session
|
|
305
|
+
// that is not a parameter (a module session, `db.session`, `self.session`) keeps its spelling. Over symbols: a def by
|
|
306
|
+
// its symbol, a call and the send by their terms. Each file states its functions' pending sends (dbc_pend_t), its
|
|
307
|
+
// calls (dbc_call_q) and their arguments (dbc_kwarg_t, dbc_posarg_t), its defs' parameters (dbc_param_q,
|
|
308
|
+
// dbc_param_pos_q, dbc_method_q), and what follows each call (dbc_commit_after_q, dbc_call_tx_q, dbc_call_in_tx_q).
|
|
309
|
+
// dbc_pend_t(f, at, s, ...): f writes on s, then sends at `at`, outside a begin block; dbc_up_q(g, at, s): calling g
|
|
310
|
+
// runs that send with the write on g's session s uncommitted (g is f, when f never commits, or a non-committing
|
|
311
|
+
// caller of one).
|
|
312
|
+
.decl dbc_pend_t(f: symbol, at: symbol, s: symbol, sq: symbol, sk: symbol, nq: symbol, nk: symbol, mq: symbol,
|
|
313
|
+
mk: symbol)
|
|
314
|
+
dbc_pend_t(fq, att, s, sq, sk, nq, nk, mq, mk) :- dbc_send_at(at, f, _, sq, sk), dbc_write(x, f, s, nq, nk, mq, mk),
|
|
315
|
+
earlier(x, at), !dbc_unreach(x, at), !dbc_in_tx(at, _), pt_def(f, fq), pt_tm(at, att).
|
|
316
|
+
.decl dbc_up_q(g: symbol, at: symbol, s: symbol) cross_file
|
|
317
|
+
dbc_up_q(fq, att, s) :- dbc_pend_t(fq, att, s, sq, sk, nq, nk, mq, mk), dbc_holds(sq, sk), dbc_holds(nq, nk),
|
|
318
|
+
dbc_holds(mq, mk), !dbc_commits_q(fq).
|
|
319
|
+
dbc_up_q(hq, att, s2) :- dbc_up_q(gq, att, s), dbc_call_q(hq, ct, gq), !dbc_commits_q(hq), dbc_arg_q(ct, gq, s, s2).
|
|
320
|
+
dbc_up_q(hq, att, s) :- dbc_up_q(gq, att, s), dbc_call_q(hq, ct, gq), !dbc_commits_q(hq), !dbc_param_q(gq, s).
|
|
321
|
+
// dbc_cc_q(at): a caller of a dbc_up_q function commits that session after the call (outside test files), or makes
|
|
322
|
+
// the call in a begin block on it.
|
|
323
|
+
.decl dbc_cc_q(at: symbol) cross_file
|
|
324
|
+
dbc_cc_q(att) :- dbc_up_q(gq, att, s), dbc_call_q(_, ct, gq), dbc_arg_q(ct, gq, s, s2),
|
|
325
|
+
dbc_commit_after_q(ct, s2, kq, kk), cel_is(kq, kk).
|
|
326
|
+
dbc_cc_q(att) :- dbc_up_q(gq, att, s), dbc_call_q(_, ct, gq), !dbc_param_q(gq, s), dbc_commit_after_q(ct, s, kq, kk),
|
|
327
|
+
cel_is(kq, kk).
|
|
328
|
+
dbc_cc_q(att) :- dbc_up_q(gq, att, s), dbc_call_q(_, ct, gq), dbc_arg_q(ct, gq, s, s2), dbc_call_tx_q(ct, s2, tq, tk),
|
|
329
|
+
cel_is(tq, tk).
|
|
330
|
+
dbc_cc_q(att) :- dbc_up_q(gq, att, s), dbc_call_q(_, ct, gq), !dbc_param_q(gq, s), dbc_call_tx_q(ct, s, tq, tk),
|
|
331
|
+
cel_is(tq, tk).
|
|
332
|
+
dbc_cc_q(att) :- dbc_up_q(gq, att, s), dbc_call_q(_, ct, gq), dbc_arg_q(ct, gq, s, s2), dbc_call_in_tx_q(ct, bt),
|
|
333
|
+
!dbc_tx_known_t(bt), dbc_sname(s2).
|
|
334
|
+
dbc_cc_q(att) :- dbc_up_q(gq, att, s), dbc_call_q(_, ct, gq), !dbc_param_q(gq, s), dbc_call_in_tx_q(ct, bt),
|
|
335
|
+
!dbc_tx_known_t(bt), dbc_sname(s).
|
|
336
|
+
// dbc_arg_q(c, g, x, s2): call c passes the argument spelled s2 for g's parameter x: by keyword, or by position
|
|
337
|
+
// (`self.m(x)`, `obj.m(x)` on a method fills `self`/`cls` first).
|
|
338
|
+
.decl dbc_arg_q(c: symbol, g: symbol, x: symbol, s2: symbol)
|
|
339
|
+
dbc_arg_q(ct, gq, x, s2) :- dbc_kwarg_t(ct, gq, x, s2), dbc_param_q(gq, x).
|
|
340
|
+
dbc_arg_q(ct, gq, x, s2) :- dbc_posarg_t(ct, gq, i, s2, "attribute"), dbc_method_q(gq), dbc_param_pos_q(gq, x, i + 1).
|
|
341
|
+
dbc_arg_q(ct, gq, x, s2) :- dbc_posarg_t(ct, gq, i, s2, "attribute"), !dbc_method_q(gq), dbc_param_pos_q(gq, x, i).
|
|
342
|
+
dbc_arg_q(ct, gq, x, s2) :- dbc_posarg_t(ct, gq, i, s2, "plain"), dbc_param_pos_q(gq, x, i).
|
|
343
|
+
// dbc_call_q(h, c, g): call c, in def h's own body, may run def g.
|
|
344
|
+
.decl dbc_call_q(h: symbol, c: symbol, g: symbol)
|
|
345
|
+
dbc_call_q(hq, ct, gq) :- call_target(c, gq), dbc_own(c, h), pt_def(h, hq), pt_tm(c, ct).
|
|
346
|
+
// dbc_kwarg_t(c, g, x, s2), dbc_posarg_t(c, g, i, s2, how): call c of g passes a name or attribute spelled s2 as
|
|
347
|
+
// keyword x, or as positional argument i, through an attribute (`obj.m(...)`) or a plain name.
|
|
348
|
+
.decl dbc_kwarg_t(c: symbol, g: symbol, x: symbol, s2: symbol)
|
|
349
|
+
dbc_kwarg_t(ct, gq, x, s2) :- call_target(c, gq), call_arg(c, -1, x, a), unparen(a, u),
|
|
350
|
+
(kind(u, "identifier") ; kind(u, "attribute")), text(u, s2), pt_tm(c, ct).
|
|
351
|
+
.decl dbc_posarg_t(c: symbol, g: symbol, i: number, s2: symbol, how: symbol)
|
|
352
|
+
dbc_posarg_t(ct, gq, i, s2, "attribute") :- call_target(c, gq), field(c, "function", fn), kind(fn, "attribute"),
|
|
353
|
+
posarg(c, i, a), unparen(a, u), (kind(u, "identifier") ; kind(u, "attribute")), text(u, s2), pt_tm(c, ct).
|
|
354
|
+
dbc_posarg_t(ct, gq, i, s2, "plain") :- call_target(c, gq), field(c, "function", fn), !kind(fn, "attribute"),
|
|
355
|
+
posarg(c, i, a), unparen(a, u), (kind(u, "identifier") ; kind(u, "attribute")), text(u, s2), pt_tm(c, ct).
|
|
356
|
+
// dbc_param_q(g, x): def g has a parameter x. dbc_param_pos_q(g, x, j): it is g's j-th positional one (from 0,
|
|
357
|
+
// `self` included). dbc_method_q(g): g is a method that takes `self` or `cls` (no staticmethod).
|
|
358
|
+
.decl dbc_param_q(g: symbol, x: symbol)
|
|
359
|
+
dbc_param_q(gq, x) :- param_named(g, x, _), pt_def(g, gq).
|
|
360
|
+
.decl dbc_param_pos_q(g: symbol, x: symbol, j: number)
|
|
361
|
+
dbc_param_pos_q(gq, x, j) :- dbc_param_pos(g, x, j), pt_def(g, gq).
|
|
362
|
+
.decl dbc_param_pos(g: node, x: symbol, j: number)
|
|
363
|
+
dbc_param_pos(g, x, j) :- sig_param(g, _, nm, i, k, _, _), (k = "pos" ; k = "posonly"), text(nm, x),
|
|
364
|
+
j = count : { sig_param(g, _, _, i2, _, _, _), i2 < i }.
|
|
365
|
+
.decl dbc_method_q(g: symbol)
|
|
366
|
+
dbc_method_q(gq) :- kind(g, "function_definition"), nearest(g, k, scope), kind(k, "class_definition"),
|
|
367
|
+
!dbc_staticmethod(g), pt_def(g, gq).
|
|
368
|
+
.decl dbc_staticmethod(g: node)
|
|
369
|
+
dbc_staticmethod(g) :- decorator(g, d0), unparen(d0, d), text(d, "staticmethod").
|
|
370
|
+
// dbc_commit_after_q(c, s2, nq, nk): outside test files, a commit of the session spelled s2 follows call c in its
|
|
371
|
+
// function. dbc_call_tx_q(c, s2, nq, nk): call c is in a begin block on s2; dbc_call_in_tx_q(c, b): in the block
|
|
372
|
+
// whose body has term b.
|
|
373
|
+
.decl dbc_commit_after_q(c: symbol, s2: symbol, nq: symbol, nk: symbol)
|
|
374
|
+
dbc_commit_after_q(ct, s2, nq, nk) :- kind(c, "call"), dbc_own(c, h), dbc_commit(k, h, s2, nq, nk), earlier(c, k),
|
|
375
|
+
!dbc_apart(k, c), docof(c, d), !cel_test_doc(d), pt_tm(c, ct).
|
|
376
|
+
.decl dbc_call_tx_q(c: symbol, s2: symbol, nq: symbol, nk: symbol)
|
|
377
|
+
dbc_call_tx_q(ct, s2, nq, nk) :- kind(c, "call"), call_target(c, _), dbc_in_tx(c, b), dbc_tx_session(b, s2, nq, nk),
|
|
378
|
+
docof(c, d), !cel_test_doc(d), pt_tm(c, ct).
|
|
379
|
+
.decl dbc_call_in_tx_q(c: symbol, b: symbol)
|
|
380
|
+
dbc_call_in_tx_q(ct, bt) :- kind(c, "call"), call_target(c, _), dbc_in_tx(c, b), docof(c, d), !cel_test_doc(d),
|
|
381
|
+
pt_tm(c, ct), pt_tm(b, bt).
|
|
382
|
+
|
|
383
|
+
// ---- 4. flush-time listeners -------------------------------------------------------------------------------------
|
|
384
|
+
.decl dbc_flush_event(e: symbol)
|
|
385
|
+
dbc_flush_event("before_insert"). dbc_flush_event("after_insert"). dbc_flush_event("before_update").
|
|
386
|
+
dbc_flush_event("after_update"). dbc_flush_event("before_delete"). dbc_flush_event("after_delete").
|
|
387
|
+
dbc_flush_event("before_flush"). dbc_flush_event("after_flush"). dbc_flush_event("after_flush_postexec").
|
|
388
|
+
dbc_flush_event("before_commit").
|
|
389
|
+
.decl dbc_event_fn(q: symbol, how: symbol)
|
|
390
|
+
dbc_event_fn("sqlalchemy.event.listens_for", "decorator"). dbc_event_fn("sqlalchemy.event.api.listens_for", "decorator").
|
|
391
|
+
dbc_event_fn("sqlalchemy.event.listen", "call"). dbc_event_fn("sqlalchemy.event.api.listen", "call").
|
|
392
|
+
// dbc_listener(f): def f listens for a flush-time event, by its decorator. dbc_listener_q(f): def f, by symbol, is
|
|
393
|
+
// registered for one by `event.listen(X, "after_insert", f)`, in its own file or another.
|
|
394
|
+
.decl dbc_listener(f: node)
|
|
395
|
+
dbc_listener(f) :- kind(f, "function_definition"), decorator(f, d0), unparen(d0, d), kind(d, "call"),
|
|
396
|
+
field(d, "function", fn), sym_of(fn, q), dbc_event_fn(q, "decorator"), posarg(d, 1, a), str(a, e),
|
|
397
|
+
dbc_flush_event(e).
|
|
398
|
+
.decl dbc_listener_q(f: symbol)
|
|
399
|
+
dbc_listener_q(fq) :- kind(c, "call"), field(c, "function", fn), sym_of(fn, q), dbc_event_fn(q, "call"),
|
|
400
|
+
posarg(c, 1, a), str(a, e), dbc_flush_event(e), posarg(c, 2, r), sym_of(r, fq).
|
|
401
|
+
|
|
402
|
+
// The cases, `upstream/celery-practice/authored/celery-practice-dispatch-before-commit/`, read as one repository: each
|
|
403
|
+
// line ending in `# caught` is reported, and no other line.
|
|
404
|
+
.test cases exactly
|
|
405
|
+
.fixture "shop/__init__.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/__init__.py"
|
|
406
|
+
.fixture "shop/api.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/api.py"
|
|
407
|
+
.fixture "shop/callers.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/callers.py"
|
|
408
|
+
.fixture "shop/celery.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/celery.py"
|
|
409
|
+
.fixture "shop/db.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/db.py"
|
|
410
|
+
.fixture "shop/dirty.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/dirty.py"
|
|
411
|
+
.fixture "shop/django_orders.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/django_orders.py"
|
|
412
|
+
.fixture "shop/helpers.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/helpers.py"
|
|
413
|
+
.fixture "shop/jobs/__init__.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/jobs/__init__.py"
|
|
414
|
+
.fixture "shop/jobs/billing.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/jobs/billing.py"
|
|
415
|
+
.fixture "shop/listeners.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/listeners.py"
|
|
416
|
+
.fixture "shop/models.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/models.py"
|
|
417
|
+
.fixture "shop/orders.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/orders.py"
|
|
418
|
+
.fixture "shop/paths.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/paths.py"
|
|
419
|
+
.fixture "shop/pool.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/pool.py"
|
|
420
|
+
.fixture "shop/rq_jobs.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/rq_jobs.py"
|
|
421
|
+
.fixture "shop/sends.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/sends.py"
|
|
422
|
+
.fixture "shop/services.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/services.py"
|
|
423
|
+
.fixture "shop/sessions.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/sessions.py"
|
|
424
|
+
.fixture "shop/tasks.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/tasks.py"
|
|
425
|
+
.fixture "shop/tests/test_orders.py" from "../upstream/celery-practice/authored/celery-practice-dispatch-before-commit/shop/tests/test_orders.py"
|
|
426
|
+
.expect _ at "shop/callers.py":14
|
|
427
|
+
.expect _ at "shop/callers.py":29
|
|
428
|
+
.expect _ at "shop/callers.py":38
|
|
429
|
+
.expect _ at "shop/dirty.py":17
|
|
430
|
+
.expect _ at "shop/dirty.py":24
|
|
431
|
+
.expect _ at "shop/dirty.py":31
|
|
432
|
+
.expect _ at "shop/dirty.py":38
|
|
433
|
+
.expect _ at "shop/dirty.py":45
|
|
434
|
+
.expect _ at "shop/dirty.py":52
|
|
435
|
+
.expect _ at "shop/dirty.py":59
|
|
436
|
+
.expect _ at "shop/listeners.py":11
|
|
437
|
+
.expect _ at "shop/listeners.py":15
|
|
438
|
+
.expect _ at "shop/listeners.py":25
|
|
439
|
+
.expect _ at "shop/orders.py":15
|
|
440
|
+
.expect _ at "shop/orders.py":21
|
|
441
|
+
.expect _ at "shop/orders.py":22
|
|
442
|
+
.expect _ at "shop/orders.py":29
|
|
443
|
+
.expect _ at "shop/orders.py":31
|
|
444
|
+
.expect _ at "shop/orders.py":40
|
|
445
|
+
.expect _ at "shop/orders.py":46
|
|
446
|
+
.expect _ at "shop/orders.py":52
|
|
447
|
+
.expect _ at "shop/orders.py":58
|
|
448
|
+
.expect _ at "shop/orders.py":65
|
|
449
|
+
.expect _ at "shop/paths.py":10
|
|
450
|
+
.expect _ at "shop/paths.py":18
|
|
451
|
+
.expect _ at "shop/paths.py":28
|
|
452
|
+
.expect _ at "shop/paths.py":37
|
|
453
|
+
.expect _ at "shop/paths.py":46
|
|
454
|
+
.expect _ at "shop/paths.py":55
|
|
455
|
+
.expect _ at "shop/paths.py":68
|
|
456
|
+
.expect _ at "shop/sends.py":31
|
|
457
|
+
.expect _ at "shop/sends.py":37
|
|
458
|
+
.expect _ at "shop/sends.py":43
|
|
459
|
+
.expect _ at "shop/sends.py":49
|
|
460
|
+
.expect _ at "shop/sends.py":55
|
|
461
|
+
.expect _ at "shop/sends.py":56
|
|
462
|
+
.expect _ at "shop/services.py":12
|
|
463
|
+
.expect _ at "shop/services.py":21
|
|
464
|
+
.expect _ at "shop/sessions.py":15
|
|
465
|
+
.expect _ at "shop/sessions.py":22
|
|
466
|
+
.expect _ at "shop/sessions.py":28
|
|
467
|
+
.end
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
.policy "celery-practice-pickle-serializer" revision 1
|
|
2
|
+
.description "Keep Celery off pickle for task messages and results; a worker that accepts pickle runs code from any message on the broker."
|
|
3
|
+
.language python
|
|
4
|
+
.mode steer
|
|
5
|
+
.scope include "**/*.py"
|
|
6
|
+
// Rerail's own rule, not a port. A worker that accepts pickle unpickles whatever a message on the broker holds, so
|
|
7
|
+
// anyone who can write to the broker (a shared Redis, a leaked URL) runs code in every worker that takes the message.
|
|
8
|
+
// Celery 4.0 made JSON the default for that reason, and its security guide says to keep pickle out of
|
|
9
|
+
// accept_content (https://docs.celeryq.dev/en/stable/userguide/security.html#serializers).
|
|
10
|
+
// Reported, at the value that names pickle ("pickle" or its MIME type "application/x-python-serialize", a list, tuple
|
|
11
|
+
// or set holding one, or a module constant of the same file holding either):
|
|
12
|
+
// - the settings task_serializer, result_serializer, event_serializer set to it, and accept_content or
|
|
13
|
+
// result_accept_content holding it, in every spelling lib/celery's cel_setting reads: `app.conf.x = v`,
|
|
14
|
+
// `app.conf["x"] = v`, `app.conf.update(x=v)` or `({"x": v})`, `Celery(..., x=v)`, Django's `CELERY_X = v`, and
|
|
15
|
+
// bare keys in a class or module named for Celery (`class CeleryConfig:`, celeryconfig.py);
|
|
16
|
+
// - a task's own `serializer=` option (`@app.task(serializer="pickle")`, `@shared_task(...)`), and
|
|
17
|
+
// `serializer = "pickle"` in the body of a class deriving from celery's Task;
|
|
18
|
+
// - a send's `serializer=` option: `t.apply_async(..., serializer="pickle")`, `app.send_task(..., serializer=...)`.
|
|
19
|
+
// app.conf.accept_content = ["json", "pickle"] reported, at "pickle"
|
|
20
|
+
// CELERY_TASK_SERIALIZER = PICKLE (PICKLE = "pickle") reported, at PICKLE
|
|
21
|
+
// app.conf.task_serializer = "json" not reported
|
|
22
|
+
// Left alone: other serializer names, among them a custom signed one (`"signed-pickle"`,
|
|
23
|
+
// `"application/x-signed-pickle"`, paperless-ngx's HMAC-signed serializer); json, msgpack and yaml; every run that
|
|
24
|
+
// calls `app.setup_security(...)` outside tests (message signing: only signed messages are accepted); test code and
|
|
25
|
+
// test settings files (cel_test_doc, and settings files named `test*.py` or `*_test*.py`); a bare `task_serializer =`
|
|
26
|
+
// outside a class or module named for Celery; `serializer="pickle"` on calls other than a Celery task's apply_async
|
|
27
|
+
// and send_task (cache clients, a multiprocessing pool's apply_async, other libraries).
|
|
28
|
+
// Gaps: values read from the environment or another module's constant; a serializer registered under another name
|
|
29
|
+
// that wraps pickle (kombu's register()); `options={"serializer": ...}`, `.set(serializer=...)` on a signature, a
|
|
30
|
+
// config dict passed to config_from_object, and `app.conf.update(**{...})`.
|
|
31
|
+
|
|
32
|
+
#include "lib/celery.rerail"
|
|
33
|
+
|
|
34
|
+
.violation PickleSerializer(at: node, k: symbol) "`{k}` enables pickle, so anyone who can write to the broker runs code in the worker. Use json; if pickle must stay, sign messages with `app.setup_security()`."
|
|
35
|
+
// Each finding is stated in its own file (ps_found), with what its task or app needs of other files (lib/celery's
|
|
36
|
+
// needs); message signing is a fact of the whole run (ps_signed: cross_file).
|
|
37
|
+
PickleSerializer(at, k) :- ps_found(at, k, nq, nk), cel_is(nq, nk), !ps_signed().
|
|
38
|
+
.decl ps_found(at: node, k: symbol, nq: symbol, nk: symbol)
|
|
39
|
+
// Settings that name one serializer.
|
|
40
|
+
ps_found(at, k, "", "") :- cel_setting(k, v, s), ps_one_key(k), ps_pickle(v, at), ps_live(s).
|
|
41
|
+
// Settings that list accepted content types.
|
|
42
|
+
ps_found(at, k, "", "") :- cel_setting(k, v, s), ps_many_key(k), ps_pickle_list(v, at), ps_live(s).
|
|
43
|
+
// A task's own option.
|
|
44
|
+
ps_found(at, "serializer", nq, nk) :- cel_task_option(f, "serializer", v, nq, nk), ps_pickle(v, at), ps_live(f).
|
|
45
|
+
// A Task subclass's class attribute.
|
|
46
|
+
ps_found(at, "serializer", "", "") :- ps_task_class(c), field(c, "body", b), body_stmt(b, st, _),
|
|
47
|
+
kind(st, "expression_statement"), child(st, a, 0), kind(a, "assignment"), field(a, "left", l), text(l, "serializer"),
|
|
48
|
+
field(a, "right", v), ps_pickle(v, at), ps_live(a).
|
|
49
|
+
// A send's execution option: `apply_async` on a Celery task (lib/celery's cel_send) or any `send_task`.
|
|
50
|
+
ps_found(at, "serializer", nq, nk) :- cel_send(c, _, nq, nk), call_arg(c, -1, "serializer", v), ps_pickle(v, at),
|
|
51
|
+
ps_live(c).
|
|
52
|
+
|
|
53
|
+
.decl ps_one_key(k: symbol)
|
|
54
|
+
ps_one_key("task_serializer"). ps_one_key("result_serializer"). ps_one_key("event_serializer").
|
|
55
|
+
.decl ps_many_key(k: symbol)
|
|
56
|
+
ps_many_key("accept_content"). ps_many_key("result_accept_content").
|
|
57
|
+
// ps_pickle_name(s): the names kombu registers pickle under.
|
|
58
|
+
.decl ps_pickle_name(s: symbol)
|
|
59
|
+
ps_pickle_name("pickle"). ps_pickle_name("application/x-python-serialize").
|
|
60
|
+
// ps_pickle(e, at): e names pickle, a literal or a file-local constant; at is e.
|
|
61
|
+
.decl ps_pickle(e: node, at: node)
|
|
62
|
+
ps_pickle(e, e) :- unparen(e, u), str(u, s), ps_pickle_name(s).
|
|
63
|
+
ps_pickle(e, e) :- unparen(e, u), kind(u, "identifier"), static_value(u, s), ps_pickle_name(s).
|
|
64
|
+
// ps_pickle_list(e, at): e is a list, tuple or set holding pickle (at is that element), or a module constant of the
|
|
65
|
+
// same file bound to one (at is e).
|
|
66
|
+
.decl ps_pickle_list(e: node, at: node)
|
|
67
|
+
ps_pickle_list(e, at) :- unparen(e, u), (kind(u, "list") ; kind(u, "tuple") ; kind(u, "set")), child(u, x, _),
|
|
68
|
+
ps_pickle(x, at).
|
|
69
|
+
ps_pickle_list(e, e) :- unparen(e, u), kind(u, "identifier"), text(u, x), look(u, x, m), kind(m, "module"),
|
|
70
|
+
asg(m, x, v, _), ps_pickle_list(v, _).
|
|
71
|
+
// ps_task_class(c): c derives from celery's Task, through the run's classes (py_names' ancestors, python/names'
|
|
72
|
+
// class_anc).
|
|
73
|
+
.decl ps_task_class_q(q: symbol)
|
|
74
|
+
ps_task_class_q("celery.Task"). ps_task_class_q("celery.app.task.Task").
|
|
75
|
+
.decl ps_task_class(c: node)
|
|
76
|
+
ps_task_class(c) :- kind(c, "class_definition"), class_base(c, b), ps_task_class_q(b).
|
|
77
|
+
ps_task_class(c) :- kind(c, "class_definition"), class_anc(c, b), ps_task_class_q(b).
|
|
78
|
+
// ps_live(n): n is in code the rule reads: no test file, no test settings file.
|
|
79
|
+
.decl ps_live(n: node)
|
|
80
|
+
ps_live(n) :- docof(n, d), !cel_test_doc(d), !ps_test_settings(d).
|
|
81
|
+
.decl ps_test_settings(d: number)
|
|
82
|
+
ps_test_settings(d) :- file_name(d, s), (glob("test*.py", s) ; glob("*_test*.py", s)).
|
|
83
|
+
// ps_signed(): the run calls `<app>.setup_security(...)` outside tests: Celery then sends and accepts only signed
|
|
84
|
+
// ("auth") messages. A fact of the whole run, from each document's own calls (ps_signs): cross_file, with no column.
|
|
85
|
+
.decl ps_signed() cross_file
|
|
86
|
+
ps_signed() :- ps_signs(_).
|
|
87
|
+
.decl ps_signs(d: number)
|
|
88
|
+
ps_signs(d) :- kind(c, "call"), field(c, "function", f), attr_of(f, _, "setup_security"), docof(c, d), !cel_test_doc(d).
|
|
89
|
+
|
|
90
|
+
// The cases, `upstream/celery-practice/authored/celery-practice-pickle-serializer/`: `proj/` is one repository, in
|
|
91
|
+
// which each line ending in `# caught` is reported and no other line; `signing/` is a second one that calls
|
|
92
|
+
// app.setup_security() and is silent.
|
|
93
|
+
.test cases exactly
|
|
94
|
+
.fixture "proj/__init__.py" from "../upstream/celery-practice/authored/celery-practice-pickle-serializer/proj/__init__.py"
|
|
95
|
+
.fixture "proj/celery.py" from "../upstream/celery-practice/authored/celery-practice-pickle-serializer/proj/celery.py"
|
|
96
|
+
.fixture "proj/celeryconfig.py" from "../upstream/celery-practice/authored/celery-practice-pickle-serializer/proj/celeryconfig.py"
|
|
97
|
+
.fixture "proj/other.py" from "../upstream/celery-practice/authored/celery-practice-pickle-serializer/proj/other.py"
|
|
98
|
+
.fixture "proj/settings.py" from "../upstream/celery-practice/authored/celery-practice-pickle-serializer/proj/settings.py"
|
|
99
|
+
.fixture "proj/settings_test.py" from "../upstream/celery-practice/authored/celery-practice-pickle-serializer/proj/settings_test.py"
|
|
100
|
+
.fixture "proj/signed.py" from "../upstream/celery-practice/authored/celery-practice-pickle-serializer/proj/signed.py"
|
|
101
|
+
.fixture "proj/tasks.py" from "../upstream/celery-practice/authored/celery-practice-pickle-serializer/proj/tasks.py"
|
|
102
|
+
.fixture "proj/tests/__init__.py" from "../upstream/celery-practice/authored/celery-practice-pickle-serializer/proj/tests/__init__.py"
|
|
103
|
+
.fixture "proj/tests/test_tasks.py" from "../upstream/celery-practice/authored/celery-practice-pickle-serializer/proj/tests/test_tasks.py"
|
|
104
|
+
.expect _ at "proj/celery.py":8
|
|
105
|
+
.expect _ at "proj/celery.py":9
|
|
106
|
+
.expect _ at "proj/celery.py":10
|
|
107
|
+
.expect _ at "proj/celery.py":12
|
|
108
|
+
.expect _ at "proj/celery.py":13
|
|
109
|
+
.expect _ at "proj/celery.py":15
|
|
110
|
+
.expect _ at "proj/celery.py":16
|
|
111
|
+
.expect _ at "proj/celeryconfig.py":2
|
|
112
|
+
.expect _ at "proj/celeryconfig.py":3
|
|
113
|
+
.expect _ at "proj/settings.py":4
|
|
114
|
+
.expect _ at "proj/settings.py":5
|
|
115
|
+
.expect _ at "proj/settings.py":16
|
|
116
|
+
.expect _ at "proj/settings.py":17
|
|
117
|
+
.expect _ at "proj/settings.py":18
|
|
118
|
+
.expect _ at "proj/tasks.py":8
|
|
119
|
+
.expect _ at "proj/tasks.py":13
|
|
120
|
+
.expect _ at "proj/tasks.py":24
|
|
121
|
+
.expect _ at "proj/tasks.py":32
|
|
122
|
+
.expect _ at "proj/tasks.py":34
|
|
123
|
+
.end
|
|
124
|
+
|
|
125
|
+
.test signing exactly
|
|
126
|
+
.fixture "proj/__init__.py" from "../upstream/celery-practice/authored/celery-practice-pickle-serializer/signing/proj/__init__.py"
|
|
127
|
+
.fixture "proj/celery.py" from "../upstream/celery-practice/authored/celery-practice-pickle-serializer/signing/proj/celery.py"
|
|
128
|
+
.end
|