Bug 29087 - Holds to pull list can crash with a SQL::Abstract puke
Summary: Holds to pull list can crash with a SQL::Abstract puke
Status: Pushed to oldstable
Alias: None
Product: Koha
Classification: Unclassified
Component: Hold requests (show other bugs)
Version: Main
Hardware: All All
: P5 - low major
Assignee: Matt Blenkinsop
QA Contact: Marcel de Rooy
URL:
Keywords:
Depends on: 3142
Blocks: 38148
  Show dependency treegraph
 
Reported: 2021-09-22 21:16 UTC by Galen Charlton
Modified: 2024-10-11 02:13 UTC (History)
18 users (show)

See Also:
Change sponsored?: ---
Patch complexity: Small patch
Documentation contact:
Documentation submission:
Text to go in the release notes:
This fixes the cause of an error (SQL::Abstract::puke():...) that can occur on the holds to pull list (Circulation > Holds > Holds to pull).
Version(s) released in:
24.11.00,24.05.04,23.11.09
Circulation function:


Attachments
Bug 29087: Prevent filter_by_for_hold to crash if default holdallowed is not_allowed (3.47 KB, patch)
2021-11-19 12:28 UTC, Jonathan Druart
Details | Diff | Splinter Review
Bug 29087: Add unit tests (6.74 KB, patch)
2024-04-16 13:25 UTC, Matt Blenkinsop
Details | Diff | Splinter Review
Bug 29087: Prevent filter_by_for_hold to crash if default holdallowed is not_allowed (6.60 KB, patch)
2024-05-08 08:23 UTC, Matt Blenkinsop
Details | Diff | Splinter Review
Bug 29087: Add unit tests (6.74 KB, patch)
2024-05-08 08:23 UTC, Matt Blenkinsop
Details | Diff | Splinter Review
Bug 29087: Prevent filter_by_for_hold to crash if default holdallowed is not_allowed (6.63 KB, patch)
2024-05-08 08:33 UTC, David Nind
Details | Diff | Splinter Review
Bug 29087: Add unit tests (6.77 KB, patch)
2024-05-08 08:33 UTC, David Nind
Details | Diff | Splinter Review
Bug 29087: Prevent filter_by_for_hold to crash if default holdallowed is not_allowed (2.82 KB, patch)
2024-05-10 10:01 UTC, Matt Blenkinsop
Details | Diff | Splinter Review
Bug 29087: Add unit tests (3.18 KB, patch)
2024-05-10 10:02 UTC, Matt Blenkinsop
Details | Diff | Splinter Review
Bug 29087: (QA follow-up): Correct default rule search (848 bytes, patch)
2024-06-28 14:01 UTC, Matt Blenkinsop
Details | Diff | Splinter Review
Bug 29087: (QA follow-up): Fix QA tests (3.63 KB, patch)
2024-06-28 14:01 UTC, Matt Blenkinsop
Details | Diff | Splinter Review
Bug 29087: Prevent filter_by_for_hold to crash if default holdallowed is not_allowed (2.90 KB, patch)
2024-07-19 08:10 UTC, Marcel de Rooy
Details | Diff | Splinter Review
Bug 29087: Add unit tests (3.26 KB, patch)
2024-07-19 08:10 UTC, Marcel de Rooy
Details | Diff | Splinter Review
Bug 29087: (QA follow-up): Fix QA tests (3.74 KB, patch)
2024-07-19 08:10 UTC, Marcel de Rooy
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Galen Charlton 2021-09-22 21:16:14 UTC
Opening the holds to pull list page can crash with the following logged error:

  SQL::Abstract::puke(): [SQL::Abstract::__ANON__] Fatal: SQL::Abstract before v1.75 used to generate incorrect SQL when the -NOT IN operator was given an undef-containing list: !!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based version of SQL::Abstract will emit the logically correct SQL instead of raising this exception) at /usr/share/koha/lib/Koha/Objects.pm line 145

I've traced this to Koha::Holds->get_items_that_can_fill(). Among other things, this routine fetches a list of item types that cannot fill holds, i.e., cases where the 'holdallowed' rule value is 'not_allowed'.

However, if your default rule is to not allow holds, the @hold_not_allowed_itypes list will end up including an entry whose value is undef. When passed to the item query, doing the -not_in filter on itype will result in the crash listed above.

A quick fix might be to filter out undef from that list. That will fix the crash, but could lead to incorrect results if the default policy is to deny holds for all item types except for ones that are going to be specifically permitted.
Comment 1 Jonathan Druart 2021-11-19 12:28:00 UTC
Created attachment 127859 [details] [review]
Bug 29087: Prevent filter_by_for_hold to crash if default holdallowed is not_allowed
Comment 2 Jonathan Druart 2021-11-19 12:29:01 UTC
This seems to be the way to go, but I don't think it's ready for integration. We should have the logic moved to Koha::ItemTypes and add test coverage. Will try and come back to this later.
Comment 3 Jonathan Druart 2021-11-19 12:31:02 UTC
Galen, thanks for reporting this bug!
Are you seeing this in master or stable?
Comment 4 Ashley Johnson 2021-12-23 01:05:43 UTC
Our library system is seeing this bug as well. Had a lot of staff reporting the error message to me today.
Comment 5 Liz Rea 2022-03-25 17:53:13 UTC
We have some libraries seeing this now as well, the rule mentioned here doesn't seem to be the culprit, but the error is the same.
Comment 6 Jonathan Druart 2022-03-25 18:42:38 UTC
Which version? Did you try the patch?
Comment 7 David Roberts 2022-10-21 02:21:53 UTC
I've tried patching a customer site with this and it works OK. Happy to sign off (even though the bug isn't requesting it yet....)
Comment 8 Katrin Fischer 2023-06-24 18:21:14 UTC
Should we move this to NSO or SO even?
Comment 9 Martin Renvoize (ashimema) 2023-09-19 15:29:40 UTC
We see this occasionally on customer sites too.. we've even got it backported in a couple of cases I think.
Comment 10 Jonathan Druart 2023-09-20 07:13:12 UTC
(In reply to Katrin Fischer from comment #8)
> Should we move this to NSO or SO even?

No, see comment 2.
Comment 11 Matt Blenkinsop 2024-04-16 13:25:03 UTC
Created attachment 164936 [details] [review]
Bug 29087: Add unit tests

prove t/db_dependent/Koha/Items.t
Comment 12 Matt Blenkinsop 2024-04-16 13:27:09 UTC
We're seeing this again on customer sites. Looking at the patch attached I'm not sure the code belongs in ItemTypes as the error is coming from the default circulation rules at its root. I've had a go at some unit tests for the patch Joubu did earlier. Would be good to get this resolved and pushed
Comment 13 David Nind 2024-04-29 09:53:53 UTC
Not sure from the comments if this is ready for sign off, or if more discussion is required.
Comment 14 Jonathan Druart 2024-05-07 13:04:06 UTC
(In reply to David Nind from comment #13)
> Not sure from the comments if this is ready for sign off, or if more
> discussion is required.

Yes, ready for signoff, we can move to a module later if needed.
Comment 15 David Nind 2024-05-08 08:20:14 UTC
The patch no longer applies:

git bz apply 29087

Bug 29087 - Holds to pull list can crash with a SQL::Abstract puke

127859 - Bug 29087: Prevent filter_by_for_hold to crash if default holdallowed is not_allowed
164936 - Bug 29087: Add unit tests

Apply? [(y)es, (n)o, (i)nteractive] y
Applying: Bug 29087: Prevent filter_by_for_hold to crash if default holdallowed is not_allowed
Using index info to reconstruct a base tree...
M	Koha/Items.pm
M	t/db_dependent/Koha/Holds.t
M	t/db_dependent/Koha/Items.t
Falling back to patching base and 3-way merge...
Auto-merging t/db_dependent/Koha/Items.t
CONFLICT (content): Merge conflict in t/db_dependent/Koha/Items.t
Auto-merging t/db_dependent/Koha/Holds.t
Auto-merging Koha/Items.pm
error: Failed to merge in the changes.
Patch failed at 0001 Bug 29087: Prevent filter_by_for_hold to crash if default holdallowed is not_allowed
Comment 16 Matt Blenkinsop 2024-05-08 08:23:55 UTC
Created attachment 166354 [details] [review]
Bug 29087: Prevent filter_by_for_hold to crash if default holdallowed is not_allowed
Comment 17 Matt Blenkinsop 2024-05-08 08:23:58 UTC
Created attachment 166355 [details] [review]
Bug 29087: Add unit tests

prove t/db_dependent/Koha/Items.t
Comment 18 David Nind 2024-05-08 08:33:27 UTC
Created attachment 166359 [details] [review]
Bug 29087: Prevent filter_by_for_hold to crash if default holdallowed is not_allowed

Signed-off-by: David Nind <david@davidnind.com>
Comment 19 David Nind 2024-05-08 08:33:30 UTC
Created attachment 166360 [details] [review]
Bug 29087: Add unit tests

prove t/db_dependent/Koha/Items.t

Signed-off-by: David Nind <david@davidnind.com>
Comment 20 David Nind 2024-05-08 08:36:58 UTC
Thanks Matt!

Testing notes (using KTD):

1. Tests pass before after the patches. 

2. Hope that is sufficient for sign off, otherwise please change the status back to needs sign off.
Comment 21 Jonathan Druart 2024-05-08 12:46:39 UTC
There is something wrong with the first patch. My initial patch did not add that many tests.
Comment 22 Matt Blenkinsop 2024-05-10 09:07:47 UTC
Will be a rebase error on my part, having a look now
Comment 23 Matt Blenkinsop 2024-05-10 10:01:58 UTC
Created attachment 166527 [details] [review]
Bug 29087: Prevent filter_by_for_hold to crash if default holdallowed is not_allowed

Signed-off-by: David Nind <david@davidnind.com>
Comment 24 Matt Blenkinsop 2024-05-10 10:02:01 UTC
Created attachment 166528 [details] [review]
Bug 29087: Add unit tests

prove t/db_dependent/Koha/Items.t

Signed-off-by: David Nind <david@davidnind.com>
Comment 25 Matt Blenkinsop 2024-05-10 10:03:03 UTC
Rebased so that the first commit matches the original, except for changes in Items.t which are now covered in the second commit
Comment 26 Marcel de Rooy 2024-06-28 08:14:27 UTC
 WARN   Koha/Items.pm
   WARN   tidiness
                The file is less tidy than before (bad/messy lines before: 136, now: 140)

 WARN   t/db_dependent/Koha/Holds.t
   WARN   tidiness
                The file is less tidy than before (bad/messy lines before: 162, now: 164)

 WARN   t/db_dependent/Koha/Items.t
   WARN   tidiness
                The file is less tidy than before (bad/messy lines before: 393, now: 397)
Comment 27 Marcel de Rooy 2024-06-28 09:21:13 UTC
-    my @hold_not_allowed_itypes = Koha::CirculationRules->search(
+    my $default_rule = Koha::CirculationRules->get_effective_rule(
         {
             rule_name    => 'holdallowed',
-            branchcode   => undef,
-            categorycode => undef,
             rule_value   => 'not_allowed',
         }
-    )->get_column('itemtype');
+    );

Shouldnt you look here for branch undef, category undef AND itemtype undef ?
Than you know that there is a 'real' default rule that does not allow holds.

If there is no such default rule but there is one itemtype with not_allowed, the if default_rule branch may calculate incorrectly?
It would get all itemtypes and subtract the ones which have an allowed rule, but they may not be present since you only reject one itemtype..
Comment 28 Matt Blenkinsop 2024-06-28 14:01:25 UTC
Created attachment 168241 [details] [review]
Bug 29087: (QA follow-up): Correct default rule search

This patch includes a check that branchcode, categorycode and itemtype are null for the default rule
Comment 29 Matt Blenkinsop 2024-06-28 14:01:28 UTC
Created attachment 168242 [details] [review]
Bug 29087: (QA follow-up): Fix QA tests
Comment 30 Marcel de Rooy 2024-07-19 07:12:03 UTC
Holds test failed with me at first, but probably related to bad data.
Fresh master passes.

prove  t/db_dependent/Koha/Items.t
All tests successful.
Files=1, Tests=20, 18 wallclock secs ( 0.08 usr  0.01 sys + 14.81 cusr  2.00 csys = 16.90 CPU)
Result: PASS
prove  t/db_dependent/Koha/Holds.t
All tests successful.
Files=1, Tests=12, 16 wallclock secs ( 0.05 usr  0.01 sys + 12.97 cusr  2.24 csys = 15.27 CPU)
Result: PASS

Apart from some noise in Items.t:  Use of uninitialized value in string eq at /usr/share/koha/Koha/Item.pm line 102.
=> $self->barcode(undef) if $self->barcode eq '';
Not coming from here.
Comment 31 Marcel de Rooy 2024-07-19 08:09:05 UTC
Can't really follow my own comment27 anymore ;) There seems to be no need for explicit undef passing to effective_rule. Will squash and edit.
Comment 32 Marcel de Rooy 2024-07-19 08:10:40 UTC
Created attachment 169179 [details] [review]
Bug 29087: Prevent filter_by_for_hold to crash if default holdallowed is not_allowed

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Comment 33 Marcel de Rooy 2024-07-19 08:10:42 UTC
Created attachment 169180 [details] [review]
Bug 29087: Add unit tests

prove t/db_dependent/Koha/Items.t

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Comment 34 Marcel de Rooy 2024-07-19 08:10:46 UTC
Created attachment 169181 [details] [review]
Bug 29087: (QA follow-up): Fix QA tests

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
[EDIT] Conform BZ comment31
Comment 35 Martin Renvoize (ashimema) 2024-07-22 06:37:53 UTC
Thanks for all the hard work!

Pushed to main for the next 24.11.00 release as RM Assistant
Comment 36 Lucas Gass (lukeg) 2024-08-28 19:42:33 UTC
Backported to 24.05.x for upcoming 24.05.04
Comment 37 Fridolin Somers 2024-09-10 13:11:06 UTC
Pushed to 23.11.x for 23.11.09
Comment 38 Wainui Witika-Park 2024-09-18 03:59:46 UTC
Not backporting to 23.05.x unless requested
Comment 39 Andreas Jonsson 2024-10-10 07:43:17 UTC
This does not work!  The  method get_effective_rule cannot be used to filter on rule_value!

my $default_rule = Koha::CirculationRules->get_effective_rule(
 	        {
 	            rule_name    => 'holdallowed',
 	            rule_value   => 'not_allowed',
 	        }
 	    );
print Dumper($default_rule->unblessed);

$VAR1 = {
          'categorycode' => undef,
          'branchcode' => undef,
          'rule_value' => 'from_any_library',
          'id' => 268,
          'itemtype' => undef,
          'rule_name' => 'holdallowed'
        };