Skip to content

Prevent deleting a printer that is in use - #1655

Merged
michaelrsweet merged 2 commits into
OpenPrinting:masterfrom
nmuggli:prevent-invalid-deletion
Jul 31, 2026
Merged

Prevent deleting a printer that is in use#1655
michaelrsweet merged 2 commits into
OpenPrinting:masterfrom
nmuggli:prevent-invalid-deletion

Conversation

@nmuggli

@nmuggli nmuggli commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

If the thread create_local_bg_thread is currently using a printer but the printer takes longer than 300 seconds to respond, the printer will get deleted by cupsdDeleteTemporaryPrinters. If the printer then responds, the background thread will be referencing an invalid pointer.

Add a helper method to check to see if any background thread is using a printer before deleting it.

If the thread create_local_bg_thread is currently using a printer but
the printer takes longer than 300 seconds to respond, the printer will
get deleted by cupsdDeleteTemporaryPrinters.  If the printer then
responds, the background thread will be referencing an invalid pointer.

Add a helper method to check to see if any background thread is using a
printer before deleting it.

@michaelrsweet michaelrsweet 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.

I agree we should do something about this, but I'm concerned about the performance of your proposed "printer is in use by client" function.

@michaelrsweet michaelrsweet self-assigned this Jul 30, 2026
@michaelrsweet michaelrsweet added bug Something isn't working investigating Investigating the issue labels Jul 30, 2026
@michaelrsweet michaelrsweet added this to the v2.4.x milestone Jul 30, 2026
@nmuggli

nmuggli commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

I considered creating a counter for the printer in use, or maybe just a bool would be good enough. But then we just need to make sure it's thread safe. I thought the 'is printer in use' function might be simpler, though not as efficient. I can get another option together and see what you think.

If the thread create_local_bg_thread is currently using a printer but
the printer takes longer than 300 seconds to respond, the printer will
get deleted by cupsdDeleteTemporaryPrinters.  If the printer then
responds, the background thread will be referencing an invalid pointer.

Add a counter to the printer struct to keep track of how many background
threads are using it.  Only delete a printer if it's unused.
Comment thread scheduler/printers.c
for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); p; p = (cupsd_printer_t *)cupsArrayNext(Printers))
{
if (p->temporary && !printer_in_use_by_client(p) &&
if (p->temporary && p->use == 0 &&

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't do any locking here since we don't use it for p->state_time either - is that OK?

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.

Yes, given the limited use of threading it isn't necessary.

@nmuggli

nmuggli commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

Updated this to do reference counting on the printer. If you like this better, I can squash and force-push. Just let me know. Thanks.

@nmuggli
nmuggli requested a review from michaelrsweet July 30, 2026 20:21

@michaelrsweet michaelrsweet 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.

This looks good.

Comment thread scheduler/printers.c
for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); p; p = (cupsd_printer_t *)cupsArrayNext(Printers))
{
if (p->temporary && !printer_in_use_by_client(p) &&
if (p->temporary && p->use == 0 &&

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.

Yes, given the limited use of threading it isn't necessary.

@michaelrsweet michaelrsweet removed the investigating Investigating the issue label Jul 31, 2026
@michaelrsweet
michaelrsweet merged commit 8a109df into OpenPrinting:master Jul 31, 2026
6 checks passed
@michaelrsweet

Copy link
Copy Markdown
Member

[2.4.x 807e99c] Protect against early deletion of IPP Everywhere printer (Issue #1655)

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants