Repository navigation
Description
Please verify that this bug has NOT been raised before.
- I checked and didn't find a similar issue
Describe the bug*
Related to the #12872 that preserves label-print failures in DataOutput and skips redelivery of failed jobs. The problem is a bit theoretical and ignoring it as 'too unlikely' is reasonable; it is likely that other issues have higher priority.
The #12872 fixes failure reporting and prevents printing again when a later delivery observes a recorded failure. However, label printing still has concurrency and interruption cases that can produce duplicate labels or misleading status.
Failure scenarios
| Scenario | Consequence | Did it exist with delete-on-error? |
|---|---|---|
| Two workers read the same unfinished output before either finishes | Both pass the guards and can print the same labels. | Yes. Deleting the output did not stop an execution already holding it. |
| A worker saves a stale output after another worker records failure | Progress or completion saves can clear the recorded error and replace it with apparent success. | The stale-write race existed. Previously, a stale save could recreate the deleted output; retaining outputs makes overwriting their error another manifestation. |
| Printing partially succeeds, then the worker times out or terminates | The output can remain unfinished, allowing redelivery to print the batch again. | Yes. Django-Q’s TimeoutException inherits from SystemExit, bypassing except Exception. Abrupt termination also bypasses deletion. |
A plugin marks the output complete before after_printing() fails |
The frontend can observe success and stop polling before the failure becomes visible. | Yes. Previously the later failure deleted the output instead of recording an error. |
| An output is deleted while a worker still holds it | A subsequent model save can recreate the deleted row. | Yes, through progress/completion saves, but not through the delete-on-error handler. The related PR extends this risk to failure recording via mark_failure(). |
How overlapping execution can occur
InvenTree supports multiple worker processes: the default is four, restricted to one per cluster for SQLite or without a global cache. Queue leasing normally prevents duplicate delivery, but its timer starts at dequeue, before waiting in the local worker queue.
For example:
| Time | Event |
|---|---|
| 0 s | The job is leased and placed in a local worker queue. |
| 280 s | Worker A starts printing. |
| 300 s | The queue lease expires. |
| 305 s | Worker B receives the same job and starts executing it while A is still printing. |
Worker A’s 90-second execution timeout has not expired. If neither execution has recorded completion or failure, both can pass the unfinished-output guard.
This is a reachable risk, not a reproduced queue-level incident. It was tested by forced overlapping execution. One worker in one cluster prevents this overlap, but does not prevent sequential reprinting after an unrecorded timeout or crash.
Possible fixes
- Claim jobs atomically before printing. Persist execution state and permit only one delivery to transition an unstarted job into execution.
- Treat interrupted printing as uncertain. Record explicit worker timeouts and re-raise them. Preserve the claim across termination or failure to save the result, so recovery does not automatically repeat potentially completed physical printing.
- Restrict status updates. Use conditional, field-specific updates so stale progress writes cannot clear errors, overwrite terminal results, or recreate deleted outputs.
- Publish completion after the full lifecycle succeeds. Include
after_printing(). For plugins that delegate work, distinguish successful dispatch from confirmed printing. - Preserve the original exception. Failure to update a deleted output should not replace the printing error.
Additional state checks or database transactions alone cannot guarantee exactly-once physical printing. That requires printer-side idempotency or reconciliation of uncertain outcomes.
Validation
Four deterministic probes using actual Django task/model code, isolated SQLite, and mocked printing reproduced stale-error loss, timeout redelivery, premature completion, and deleted-row recreation. These were not live multi-worker or hardware tests.
Steps to Reproduce
Overloaded server:
| Time | Event |
|---|---|
| 0 s | The job is leased and placed in a local worker queue. |
| 280 s | Worker A finished previous work and starts printing. |
| 300 s | The queue lease expires. |
| 305 s | Worker B receives the same job and starts executing it while A is still printing. |
Timeout case:
- Submit a label job that prints some labels, then stalls or continues beyond the worker’s execution timeout—90 seconds by default.
- Django-Q raises TimeoutException, which inherits from SystemExit and bypasses the print handler’s except Exception.
- The output remains unfinished, with no recorded failure; delete-on-error never runs.
- When the queue retries the job, it passes the unfinished-output guard and starts printing again, potentially duplicating labels already printed.
Expected behaviour
Print job is started only once; partial print is not restarted automatically
Deployment Method
None
Version Information
Both master and #12872 are affected
Try to reproduce on the demo site
I did not try to reproduce
Is the bug reproducible on the demo site?
Not reproducible
Relevant log output
I have reviewed this report for correctness and filled out all sections.
- I have checked this report. I acknowledge that filing bugs via AI agents without review might lead to being blocked.
Activity
matmair commentedon Sep 17, 2026
I find this highly theoretical. Is this an actual error or a theoretical AI finding?
ledvinap commentedon Sep 17, 2026
More the second, but described 90s stall during print is possible (driver error, too long job, ...). And it can have quite expensive consequences - printing again and again every 5 minutes.
It is possible to trigger it, the scenario isn't too exotic:
Confirmed with the upstream delete-on-error code: timeout causes reprinting.
The simulation used Django-Q’s real timeout handler, ORM broker, worker loop, and result monitor:
complete=False,errors=None,progress=1.Result: two requested labels produced three simulated prints.
Only the printer was mocked and lease expiry accelerated; no hardware was used.
matmair commentedon Sep 17, 2026
Have you personally been able to reproduce this with an actual, correctly production-configured InvenTree instance as per docs and a published production printer driver? Not in a simulated environment. You can mock any behavior you want, especially when constructing from a failure scenario
ledvinap commentedon Sep 17, 2026
No, and that is not how I verify programs, with or without AI.
Print job that takes over 90s will trigger it in default config (as demonstrated in #12873 (comment)).
matmair commentedon Sep 17, 2026
I believe the scenario of 90s for a print to be a constructed one and per Threat Model $4 we trust all plugins to be done correctly. Imo this is a waste of time brought up by either an AI explicitly searching for issues or by a wrongly implemented plugin being tested on a instance without debug enabled, then leading some AI loop trying to get itself out of the corener. Which would also explain #12874 and #12872 instead of just turning on label/report debug.
Which still leaves the question why your are flooding the repo with all of these individually (7 small PRs in the last 10hrs, at least 3 of them fixing non-prod issues).
ledvinap commentedon Sep 18, 2026
AI: PT-P750W + 100 × 30 mm labels is a valid supported configuration whose theoretical minimum printer time is 100 seconds. No printer fault, plugin bug, artificial sleep, or overload is required. The real InvenTree task is longer still because it also renders and rasterizes each label and performs cutting / I/O.
It started as 'existing, but low priority' issue. 90s job-timeout made it a real-world problem.
It was pointed out in unrelated AI review, but it is a real issue.
It takes much more complex problem to get current AI tools cornered.
It is result of using InvenTree. Simple changes first, complex one later.
Can you suggest what PRs can be merged, please?
#12874, #12872 isn't too visible in current IT (missing QRcode data is one possible exmple). With additional template validation added, it becomes more annoying - the problem can be in Stock item (missing serial, missing version, firmvare version installed) and not in template. User need feedback in that case.
work in my InvenTree, AI:
work in related projects, AI:
inventree-brother-plugin— printer integration and accurate label renderingThe main improvement connects InvenTree’s physical label dimensions to the printer’s actual raster geometry. This matters particularly for small labels, where scaling, rounding, or cropping can noticeably affect text and barcodes.
Two-color
62redprinting remains experimental. Accurate two-color debug visualization is not implemented.brother_ql-inventree— printer geometry, transport, and diagnosticsThis library provides the lower-level functionality used by the Brother plugin. The additions can also serve other applications generating Brother raster jobs.
inventree-adv-sheet-label— physical placement and configurable layoutsThis work targets office printers and adhesive label sheets. It models the physical sheet, label artwork, and printer margins separately.
Potentially useful standalone contributions include the sheet-size fix, configurable sheet layouts, printable-area validation, and the shared printer render-profile API. Brother-specific batch and cutting controls provide additional value for production printing.
SchrodingersGat commentedon Sep 22, 2026
@ledvinap please reduce the verbosity of these AI generated posts - they are way too cumbersome to read.
Refer to this PR - #12913 - which introduces the ability to single-shot tasks, which might be helpful here
ledvinap commentedon Sep 22, 2026
Sorry, it is just to show why I'm flooding the repo with all of these individually. Would you mind than I edit them to replace them with few sentences?
github-actions commentedon Nov 21, 2026
This issue seems stale. Please react to show this is still important.