[Bug 43167] New: Bug in C4::Ris::marc2ris: Destructive STDOUT closing breaks under Plack/PSGI (Error 500) and causes 0-byte exports when processing flawed MARC fields (e.g., 100 ind1 blank)
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43167 Bug ID: 43167 Summary: Bug in C4::Ris::marc2ris: Destructive STDOUT closing breaks under Plack/PSGI (Error 500) and causes 0-byte exports when processing flawed MARC fields (e.g., 100 ind1 blank) Initiative type: --- Sponsorship --- status: Product: Koha Version: unspecified Hardware: All OS: All Status: NEW Severity: enhancement Priority: P5 - low Component: OPAC Assignee: oleonard@myacpl.org Reporter: lopalejandro@gmail.com QA Contact: testopia@bugs.koha-community.org Target Milestone: --- In KOHA 23.05.01 when exporting certain records to RIS format via OPAC or Intranet, the server throws an HTTP 500 Error. Plack error logs show: print() on closed filehandle STDOUT at opac-export.pl. This is triggered when the marc2ris function encounters a flawed MARC record—specifically, we isolated the trigger to MARC field 100 having a blank/empty Indicator 1, which breaks the author string evaluation. When this parsing fails, it triggers a cascade effect due to legacy code in /Koha/lib/C4/Ris.pm (sub marc2ris) that attempts to redirect STDOUT destructively: open my $oldout, qw{>}, "&STDOUT"; my $outvar; close STDOUT; open STDOUT,'>:encoding(utf8)', \$outvar; Under a Plack/PSGI environment, closing STDOUT destroys the HTTP output stream handle (_GEN_...), making it impossible to restore it at the end of the function. Any subsequent print call throws an HTTP 500. Furthermore, for analytic records lacking authors (like serials) where the output size doesn't exceed the memory buffer, skipping a proper STDOUT flush before the return causes the function to return an uninitialized variable, resulting in a 0-byte RIS file. Proposed Solution: Use local *STDOUT for a safe memory redirection, and close the localized handle strictly to flush the UTF-8 buffer before returning. Remove lines: open my $oldout, qw{>}, "&STDOUT"; # ... close STDOUT; open STDOUT,'>:encoding(utf8)', \$outvar; Replace with: local *STDOUT; open STDOUT,'>:encoding(utf8)', \$outvar; At the end of the function, remove the $oldout restoration and ensure the buffer is flushed: # close STDOUT; # open STDOUT, ">&", $oldout; Replace with: close STDOUT; # Flushes the buffer into $outvar return $outvar; -- You are receiving this mail because: You are watching all bug changes.
participants (1)
-
bugzilla-daemon@bugs.koha-community.org