Skip to content

Extend timeout for background label printing tasks - #12840

Closed
wyyd-yxc wants to merge 2 commits into
inventree:masterfrom
wyyd-yxc:fix/11650-label-print-timeout
Closed

Extend timeout for background label printing tasks#12840
wyyd-yxc wants to merge 2 commits into
inventree:masterfrom
wyyd-yxc:fix/11650-label-print-timeout

Conversation

@wyyd-yxc

@wyyd-yxc wyyd-yxc commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Problem

When printing multiple labels at once (e.g. with the Brother label plugin on a remote networked printer), the print job can legitimately take longer than the default background worker timeout (90s). When the worker kills such a task, the queued task can be re-delivered and executed again - resulting in some labels being printed multiple times (and others never, as the output is marked complete). See #11650.

Per-task retries cannot be disabled (django-q2 only supports a global max_attempts, see django-q2#114), but a custom timeout per task is supported by django-q and honoured by the worker (task.pop("timeout", ...)).

Solution

  • offload_task() gains an optional timeout parameter: when provided (and the task is offloaded asynchronously), it is attached to the queued task as a task-level timeout which overrides the default worker timeout. It is not passed through to the task function itself, and it is ignored for synchronously executed tasks.
  • Label printing tasks (both the plugin path and the label machine path) are now offloaded with an extended default timeout (LABEL_PRINT_TIMEOUT = 600), so slow printer communication no longer trips the worker timeout / re-delivery behaviour.

Note on the tasks.py / CHANGELOG.md diff

This branch is based on a fork of InvenTree which cannot be synced with upstream (the sync is refused without the workflow OAuth scope, as upstream's recent commits touch workflow files). Consequently the tasks.py and CHANGELOG.md diffs also contain upstream changes made after the fork point (the PRE_1_0_0_MIGRATION_BOUNDARIES addition and recent changelog entries). The changes introduced by this PR are limited to:

  • offload_task(): the new timeout parameter (signature, docstring, and passing it to the queued task)
  • plugin/base/label/mixins.py: the LABEL_PRINT_TIMEOUT constant and its use
  • plugin/builtin/labels/inventree_machine.py: using the constant
  • the two new tests and the changelog entry

How to test

  • InvenTree.test_tasks.InvenTreeTaskTests.test_offload_task_timeout: verifies that a custom timeout lands on the queued task (not in the task kwargs), that no timeout key is set without one, and that it is ignored on the synchronous path.
  • plugin.base.label.test_label_mixin.LabelMixinTests.test_async_printing_timeout: verifies non-blocking plugin printing offloads with LABEL_PRINT_TIMEOUT.
  • machine.tests.TestLabelPrinterMachineType.test_print_label_timeout: same assertion for the label machine path.

Fixes #11650

@netlify

netlify Bot commented Sep 11, 2026

Copy link
Copy Markdown

Deploy Preview for inventree-web-pui-preview canceled.

Name Link
🔨 Latest commit 3ee1ef3
🔍 Latest deploy log https://app.netlify.com/projects/inventree-web-pui-preview/deploys/6aa439dfbdfc8c00086c480a

@matmair matmair added bug Identifies a bug which needs to be addressed backport Apply this label to a PR to enable auto-backport action backport-to-1.5.x labels Sep 11, 2026
@matmair matmair added this to the 1.6.0 milestone Sep 11, 2026
@wyyd-yxc wyyd-yxc closed this Sep 11, 2026
@wyyd-yxc
wyyd-yxc force-pushed the fix/11650-label-print-timeout branch from 39c44d8 to 7cd74ee Compare September 11, 2026 16:51
@matmair

matmair commented Sep 11, 2026

Copy link
Copy Markdown
Member

@wyyd-yxc this structure does not really make sense or fix the repo; please revise

Label printing tasks which are run in the background worker may
legitimately take longer than the default worker timeout (e.g. when
printing to a slow remote networked printer). If such a task is killed
by the worker timeout it may be re-delivered, causing labels to be
printed multiple times.

Add a per-task timeout parameter to offload_task() and run label
printing tasks with an extended default timeout.

Note: the tasks.py portion of this diff includes upstream changes made
after the fork this branch is based on was last synced (the fork cannot
be synced without the workflow OAuth scope). The changes introduced by
this PR in tasks.py are limited to the offload_task() timeout parameter.

Fixes inventree#11650
@wyyd-yxc wyyd-yxc reopened this Sep 11, 2026
@wyyd-yxc
wyyd-yxc requested a review from matmair as a code owner September 11, 2026 17:26
@matmair

matmair commented Sep 11, 2026

Copy link
Copy Markdown
Member

@wyyd-yxc stop force pushing, it makes it much harder to find context on comments

@matmair matmair left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there seems to be some issue with some code from other branches being included

Comment thread CHANGELOG.md

### Changed

- [#12840](https://github.com/inventree/InvenTree/pull/12840) increases the default timeout for background label printing tasks. Slow print jobs (e.g. when printing to remote networked printers) are no longer killed and re-delivered by the background worker, which could result in labels being printed multiple times.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not changelog worthy


# The first and last individual migrations of each pre-1.0.0 squash range,
# for every app squashed as part of the pre-1.0.0 migration-history cleanup.
PRE_1_0_0_MIGRATION_BOUNDARIES = [

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please cleanup the diff

machine = self.create_machine('test-label-printer-api')

# setup the label app
apps.get_app_config('report').create_default_labels()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isnt this redudant?

Comment on lines +330 to +335
plugin_module = type(
plg_registry.get_plugin(plugin_ref, active=None)
).print_labels.__globals__['__name__']

with mock.patch(
f'{plugin_module}.offload_task', return_value=None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cant you just refer to this statically?

Comment on lines +18 to +23
# Timeout (in seconds) for label printing tasks which are run in the background.
# Printing (especially to remote networked printers) can take longer than the
# default background worker timeout. If a print task is killed by the worker
# timeout it may be re-delivered, resulting in duplicate prints.
# See https://github.com/inventree/InvenTree/issues/11650
LABEL_PRINT_TIMEOUT = 600

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a short comment and the link is enough

@@ -220,6 +220,48 @@ def test_printing_process(self):
# And that it is a valid image file
Image.open(f'{test_path}.png')

def test_async_printing_timeout(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what doe this test offer that other does not?

Comment thread CHANGELOG.md

### Breaking Changes

- [#12830](https://github.com/inventree/InvenTree/pull/12830) squashes all database migrations prior to the 1.0.0 release. This means that any users who are updating from a version older than 1.0.0 must first update to the 1.0.0 release before updating to the current release.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleanup please

@wyyd-yxc

Copy link
Copy Markdown
Contributor Author

Sorry for the noise here - the branch picked up some unrelated changes from my out-of-date fork, and I made things worse by re-writing the branch history. Closing this for now. If a longer default timeout for label print tasks is wanted on the core side I'm happy to give it another go with a clean single commit.

@wyyd-yxc

Copy link
Copy Markdown
Contributor Author

null

@wyyd-yxc wyyd-yxc closed this Sep 11, 2026
@codecov

codecov Bot commented Sep 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.35593% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.84%. Comparing base (7cd74ee) to head (3ee1ef3).
⚠️ Report is 7 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12840      +/-   ##
==========================================
- Coverage   87.20%   86.84%   -0.37%     
==========================================
  Files        1487     1500      +13     
  Lines      101455   102462    +1007     
  Branches    11639    11639              
==========================================
+ Hits        88473    88980     +507     
- Misses      12919    13419     +500     
  Partials       63       63              
Flag Coverage Δ
backend 91.81% <81.35%> (+0.60%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Backend Apps 91.82% <100.00%> (-0.68%) ⬇️
Backend General 90.37% <ø> (-4.37%) ⬇️
Frontend 79.77% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@matmair

matmair commented Sep 11, 2026

Copy link
Copy Markdown
Member

the general approach looked right but we can not merge it this way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport Apply this label to a PR to enable auto-backport action backport-to-1.5.x bug Identifies a bug which needs to be addressed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

label printing background task timeouts - labels get printed multiple times

2 participants