[Bug 43020] New: One bad record can abort an entire OAI-PMH harvest
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43020 Bug ID: 43020 Summary: One bad record can abort an entire OAI-PMH harvest Initiative type: --- Sponsorship --- status: Product: Koha Version: Main Hardware: All OS: All Status: NEW Severity: normal Priority: P5 - low Component: Cataloging Assignee: koha-bugs@lists.koha-community.org Reporter: martin.renvoize@openfifth.co.uk QA Contact: testopia@bugs.koha-community.org CC: m.de.rooy@rijksmuseum.nl Depends on: 35104 Target Milestone: --- Koha::OAI::Client::Harvester::processRecord has no error handling around MARC::Record->new_from_xml when decoding a harvested record's metadata, and no handling for Koha::Exceptions::Metadata::Invalid around the subsequent AddBiblio/ModBiblio/AddAuthority/ModAuthority calls either. Since init()'s harvest loop has no try/catch around processRecord, either failure dies uncaught, aborting the entire harvest_oai.pl cron run partway through - no completion log entry, no report email, and every remaining record in that batch is left unprocessed. This was found while auditing other places in the codebase that ingest externally-sourced MARC data, prompted by bug 35104 (which fixed the same class of problem - one bad record silently aborting a whole batch/job - in the staged MARC import commit/revert code paths). Test plan: 1. Configure an OAI-PMH server under Administration > OAI-PMH harvesting servers, with a record type of biblio. 2. Arrange for one harvested record to have metadata that cannot be decoded as MARCXML (e.g. an empty <datafield> element, or any malformed MARC). 3. Run: misc/cronjobs/harvest_oai.pl -r <server id> 4. Before this patch: the cron job dies partway through, with no harvest report and no indication of which record caused it. 5. After this patch: the harvest completes. The bad record is counted as in_error in the harvest report/email, its identifier and decoding error are logged, and every other record in the batch is still processed normally. 6. prove t/db_dependent/Koha/OAIHarvester.t Referenced Bugs: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35104 [Bug 35104] We should warn when attempting to save MARC records that contain characters invalid in XML -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43020 --- Comment #1 from Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> --- Created attachment 201689 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=201689&action=edit Bug 43020: Do not let one bad OAI-harvested record kill the whole harvest Koha::OAI::Client::Harvester::processRecord called MARC::Record->new_from_xml on harvested metadata with no error handling, and AddBiblio/ModBiblio/AddAuthority/ModAuthority with no handling for Koha::Exceptions::Metadata::Invalid either. Since init()'s harvest loop has no try/catch around processRecord, either failure died uncaught, aborting the whole harvest_oai.pl cron run partway through - no completion log entry, no report email, and every remaining record in that batch left unprocessed. processRecord now catches both cases the same way it already handles an XSLT transform failure a few lines above: log the record identifier and error, report status 'in_error' for that one record, and let the harvest continue with the rest. -- You are receiving this mail because: You are watching all bug changes. You are the assignee for the bug.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43020 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Assignee|koha-bugs@lists.koha-commun |martin.renvoize@openfifth.c |ity.org |o.uk Status|NEW |Needs Signoff -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43020 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |dcook@prosentient.com.au -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43020 --- Comment #2 from David Cook <dcook@prosentient.com.au> --- Would be good to have a test plan that's easier to follow. I've got an idea though... -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43020 --- Comment #3 from David Cook <dcook@prosentient.com.au> --- Sorry but I'm not seeing the difference before/after the patch. I'm going to need more information to test :( -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43020 --- Comment #4 from David Cook <dcook@prosentient.com.au> --- For testing, I used "nc" to listen: nc -C -N -k -l -p 5000 -v And then I manually sent responses like these: HTTP/1.1 200 OK Content-Type: text/xml; charset=UTF-8 Connection: close <?xml version="1.0" encoding="UTF-8"?> <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/"> <responseDate>2026-08-06T02:58:00Z</responseDate> <request>http://localhost:5000/oaitest</request> </OAI-PMH> HTTP/1.1 200 OK Content-Type: text/xml; charset=UTF-8 Connection: close <?xml version="1.0" encoding="UTF-8"?> <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"> <responseDate>2026-08-06T03:00:00Z</responseDate> <request verb="ListMetadataFormats">http://localhost:5000/oaitest</request> <ListMetadataFormats> <metadataFormat> <metadataPrefix>marcxml</metadataPrefix> <schema>http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd</schema> <metadataNamespace>http://www.loc.gov/MARC21/slim</metadataNamespace> </metadataFormat> </ListMetadataFormats> </OAI-PMH> HTTP/1.1 200 OK Content-Type: text/xml; charset=UTF-8 Connection: close <?xml version="1.0" encoding="UTF-8"?> <OAI-PMH xsi:schemaLocation='http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd' xmlns='http://www.openarchives.org/OAI/2.0/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> <responseDate>2026-08-06T02:48:47Z</responseDate> <request metadataPrefix='marcxml' verb='ListRecords'>http://localhost:5000/oaitest</request> <ListRecords> <record> <header><identifier>oaitest:1</identifier><datestamp>2026-08-06T02:46:52Z</datestamp></header> <metadata> <record xmlns='http://www.loc.gov/MARC21/slim' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd'> <leader>01344cam a22003014i 4500</leader> <controlfield tag='001'>1</controlfield> <controlfield tag='005'>20200129130600.0</controlfield> <controlfield tag='008'>120829t20132012nyu bk 001 0ceng </controlfield> <datafield ind1='1' ind2='0' tag='245'> <subfield code='a'>Test title :</subfield> <subfield code='b'>test subtitle /</subfield> <subfield code='c'>Test author.</subfield> </datafield> </record> </metadata> </record> <record> <header><identifier>oaitest:2</identifier><datestamp>2026-08-06T02:46:52Z</datestamp></header> <metadata> <record xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://www.loc.gov/MARC21/slim' xsi:schemaLocation='http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd'> <leader>00879pam a2200265 a 4500</leader> <controlfield tag='001'>2</controlfield> <controlfield tag='005'>20200421093816.0</controlfield> <controlfield tag='008'>900202s1990 nyua 001 0 eng </controlfield> <datafield tag="100" ind1="" ind2=""/> <datafield tag='245' ind2='4' ind1='1'> <subfield code='a'>Bad record /</subfield> <subfield code='c'></subfield> </datafield> <datafield tag='1000' ind2=' ' ind1=' '> <subfield code='a'></subfield> </datafield> </record> </metadata> </record> <record> <header><identifier>oaitest:3</identifier><datestamp>2026-08-06T02:46:52Z</datestamp></header> <metadata> <record xmlns='http://www.loc.gov/MARC21/slim' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd'> <leader>01344cam a22003014i 4500</leader> <controlfield tag='001'>3</controlfield> <controlfield tag='005'>20200129130600.0</controlfield> <controlfield tag='008'>120829t20132012nyu bk 001 0ceng </controlfield> <datafield ind1='1' ind2='0' tag='245'> <subfield code='a'>Test title 2:</subfield> <subfield code='b'>test subtitle 2/</subfield> <subfield code='c'>Test author 2.</subfield> </datafield> </record> </metadata> </record> </ListRecords> </OAI-PMH> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43020 --- Comment #5 from David Cook <dcook@prosentient.com.au> --- 3 records get imported into Koha but they're junk -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43020 Adolfo Rodríguez Taboada <adolfo.rodriguez@xercode.es> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|Needs Signoff |Signed Off -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43020 Adolfo Rodríguez Taboada <adolfo.rodriguez@xercode.es> changed: What |Removed |Added ---------------------------------------------------------------------------- Attachment #201689|0 |1 is obsolete| | -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43020 --- Comment #6 from Adolfo Rodríguez Taboada <adolfo.rodriguez@xercode.es> --- Created attachment 211634 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=211634&action=edit Bug 43020: Do not let one bad OAI-harvested record kill the whole harvest Koha::OAI::Client::Harvester::processRecord called MARC::Record->new_from_xml on harvested metadata with no error handling, and AddBiblio/ModBiblio/AddAuthority/ModAuthority with no handling for Koha::Exceptions::Metadata::Invalid either. Since init()'s harvest loop has no try/catch around processRecord, either failure died uncaught, aborting the whole harvest_oai.pl cron run partway through - no completion log entry, no report email, and every remaining record in that batch left unprocessed. processRecord now catches both cases the same way it already handles an XSLT transform failure a few lines above: log the record identifier and error, report status 'in_error' for that one record, and let the harvest continue with the rest. Signed-off-by: Adolfo Rodríguez <adolfo.rodriguez@xercode.es> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43020 Adolfo Rodríguez Taboada <adolfo.rodriguez@xercode.es> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |adolfo.rodriguez@xercode.es --- Comment #7 from Adolfo Rodríguez Taboada <adolfo.rodriguez@xercode.es> --- Hi David, I think I found why you couldn't see a difference: it depends on whether the OAI server has an XSLT configured. Without an XSLT, HTTP::OAI returns the metadata as a DocumentFragment rooted at <metadata>. MARC::File::XML only reads fields that are direct children of the root, so it keeps the leader and nothing else. Every record gets imported as an empty shell (leader + 999), and nothing can fail to decode. That's the "3 records get imported but they're junk" you saw. The unit test passes the metadata as a string, so it doesn't hit this path. The empty records look like a separate problem. With an XSLT that extracts the MARC record, the bad record does reach MARC::Record->new_from_xml, and the patch makes a difference. I used this one: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:marc="http://www.loc.gov/MARC21/slim"> <xsl:output method="xml" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <marc:collection><xsl:copy-of select="//marc:record"/></marc:collection> </xsl:template> </xsl:stylesheet> Test plan: Serve three records from a fake OAI-PMH endpoint, where the second one has an empty datafield (e.g. <datafield tag="100" ind1="1" ind2=" "/>). Add an OAI server for it with record type biblio, format marcxml, and the XSLT above. Run misc/cronjobs/harvest_oai.pl -v -r <id>. Before the patch, record 1 is added, then the script dies with "Field 100 must have at least one subfield". Record 3 is never processed and there's no harvest report. After the patch: the harvest completes with "2 added, 1 in_error"; the log shows "Record <identifier> could not be decoded: Field 100 must have at least one subfield"; the script exits normally; records 1 and 3 are imported in full, with their title; running it again with -f gives "2 updated, 1 in_error". QA tools and t/db_dependent/Koha/OAIHarvester.t pass. -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43020 Lisette Scheer <lisette@bywatersolutions.com> changed: What |Removed |Added ---------------------------------------------------------------------------- QA Contact|testopia@bugs.koha-communit |lisette@bywatersolutions.co |y.org |m -- You are receiving this mail because: You are watching all bug changes.
participants (1)
-
bugzilla-daemon@bugs.koha-community.org