Skip to content

Entity writes intermittently 500 with an NPE: EntityLifecycleEventDispatcher reads its handler list unsynchronized #33711

Closed

Description

@mohityadav766

Affected module

Backend (openmetadata-service)

Describe the bug

EntityLifecycleEventDispatcher keeps its registered handlers in a plain ArrayList. The list is mutated under synchronized (registerHandler / unregisterHandler) but read with no synchronization at all on the dispatch hot path — getApplicableHandlers, getHandlers, getHandlerCount.

ArrayList#removeIf → shiftTailOverGap decrements size first and then writes null into the vacated tail slot. A dispatch already walking the list has bound its stream spliterator fence to the pre-removal size, so it walks straight into the nulled slot and the entity write fails with:

An exception with message [Cannot invoke
"org.openmetadata.service.events.lifecycle.EntityLifecycleEventHandler.getSupportedEntityTypes()"
because "handler" is null] was thrown while processing request.
at EntityLifecycleEventDispatcher.lambda$getApplicableHandlers$13(EntityLifecycleEventDispatcher.java:358)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1708)
at EntityLifecycleEventDispatcher.getApplicableHandlers(EntityLifecycleEventDispatcher.java:361)
at EntityLifecycleEventDispatcher.onEntityCreated(EntityLifecycleEventDispatcher.java:122)

VectorEmbeddingHandler has priority 200, so it always sorts last — removing it always nulls exactly the slot a concurrent walker is about to read, which is why that handler is the one that trips it.

This is not test-only. Handler registration is not startup-only:

  • SearchRepository's constructor unregisters and re-registers SearchIndexHandler.
  • SystemRepository.retryInitAndReportError → SearchRepository.initializeVectorSearchService() → OpenSearch/ElasticSearchVectorService.init unregisters and re-registers VectorEmbeddingHandler, reachable from the settings-validation API while requests are in flight.

There is a second, quieter defect at those same call sites: expressing the swap as unregister-then-register leaves a window in which no handler of that name is registered, so an entity write landing in it is silently never indexed.

To Reproduce

Surfaces as a flaky integration-test failure — openmetadata-integration-tests runs classes and methods concurrently (junit-platform.properties), and PatchTableEmbeddingIT (@Execution(CONCURRENT)) unregisters VectorEmbeddingHandler and re-inits the vector service mid-run while other suites are writing entities:

[ERROR] GlossaryTermResourceIT.test_searchGlossaryTermsWithOffsetPagination:2930->createEntity:140
  » Api An exception with message [Cannot invoke "...EntityLifecycleEventHandler.getSupportedEntityTypes()"
  because "handler" is null] was thrown while processing request.
[ERROR] Tests run: 15598, Failures: 0, Errors: 1, Skipped: 1394

Any suite creating an entity can be the victim; the failing test name varies run to run.

Deterministic reproduction (no stress loop) — park a dispatch mid-walk inside a handler's getSupportedEntityTypes(), unregister the tail handler, release:

  1. Register handler A (priority 10) whose getSupportedEntityTypes() blocks on a latch, and handler B (priority 200).
  2. On another thread call dispatcher.onEntityCreated(...); it blocks inside A.
  3. Call dispatcher.unregisterHandler("B").
  4. Release the latch — the dispatch reads the nulled slot and throws the NPE above.

Expected behavior

Registering or unregistering a lifecycle handler must never make a concurrent entity write fail, and must never leave a window in which an entity write sees no handler under a name that is being swapped.

Additional context

The unsynchronized ArrayList dates back to #21921; the removeIf call sites that make it observable landed later. Because it is interleaving-dependent it presents as a new flake rather than a new bug.

Activity

  1. added
    bug
    on Sep 21, 2026
  2. added a commit that references this issue on Sep 21, 2026
  3. mohityadav766 commented on Sep 21, 2026

    @mohityadav766
    MemberAuthor

    CI occurrence data

    Scanned the Integration Tests – PostgreSQL + OpenSearch workflow (the only matrix entry where PatchTableEmbeddingIT is not skipped) for the getSupportedEntityTypes() … because "handler" is null signature.

    Method: 734 failed runs since 2026-08-25 → sampled up to 6 runs/day → 122 of them had a genuinely failed Integration Test Lane job → grepped each of those 122 job logs.

    2 hits, both in the parallel lane:

    When Run Branch Victim
    2026-09-03 21:33 33806564136 gh-readonly-queue/main/pr-31213 (merge queue on main) FileResource create
    2026-09-21 09:12 / 09:23 35579637032 fix/33685-service-lineage-pipeline CsvExportMultiNodeIT, then GlossaryTermResourceIT.test_searchGlossaryTermsWithOffsetPagination

    Notes:

    • The victim varies — glossary term, CSV export, file create. Grepping CI for any single test name will undercount this; grep the NPE signature instead.
    • It has reached main's merge queue, not only feature branches.
    • Zero hits in the 2026-08-25 → 08-31 window (~22 lane failures sampled).

    Two caveats on reading "first seen 2026-09-03":

    1. Only ~17% of failed runs were probed, so the true occurrence count is higher than 2.
    2. A non-match only means that run did not lose the race — not that the bug was absent. 2026-09-03 is a lower bound on first observation, not the date the defect appeared.

    Code archaeology puts the actual reachability much earlier: the unsynchronized ArrayList landed in #21921, and the concurrent removeIf that makes it observable has been in PatchTableEmbeddingIT since 2026-03-10. Neither the dispatcher nor junit-platform.properties has changed since July, so nothing recent "broke" it — the interleaving simply started landing often enough to notice.

  4. mohityadav766 commented on Sep 21, 2026

    @mohityadav766
    MemberAuthor

    Follow-up: extended scan, and why no PR "introduced" this

    Extended the scan back to 2026-07-19. Totals across the OpenSearch IT workflow, Integration Test Lane failures only:

    Window Probed Hits
    2026-07-19 → 2026-09-02 157 0
    2026-09-03 → 2026-09-21 86 2 (09-03, 09-21)

    Both hits fall in the later window. P(both land there by chance) = 0.124 — suggestive, not statistically significant, so this does not establish a changepoint.

    The causal chain is in the code, not in a recent PR:

    PR When Role
    #21921 feat: implement entity lifecycle event delegation 2025-06-25 The defect. Introduced the dispatcher with the unsynchronized ArrayList. Neither the field nor the read path has changed since.
    #26356 Add full-server integration test for PATCH table embedding + semantic search 2026-03-10 Made it reachable in CI. Added the @Execution(CONCURRENT) IT calling unregisterHandler("VectorEmbeddingHandler") mid-suite — the removeIf that nulls the tail slot while other suites write entities.
    #27651 fix(it): stop flaky integration tests 2026-04-27 Added the production unregister-then-register in SearchRepository, widening exposure beyond tests.

    On #31213 (the PR whose merge-queue run produced the earliest observed hit): it merged 2026-09-04 20:07, after the 09-03 21:11 failure, so that run was its pre-merge validation. It touches neither EntityLifecycleEventDispatcher nor SearchRepository, so it cannot introduce this race.

    It is worth noting honestly that it is not irrelevant either: it wraps every OpenSearch request in MeteredOpenSearchTransport, so it does change timing across the whole OpenSearch matrix. More per-request latency means more concurrent in-flight requests, which means more chances for a dispatch to overlap the single unregister. That is a plausible window-widener, not a cause. With p=0.124 it cannot be distinguished from coincidence either way.

    Either way the fix in #33712 is engine-agnostic and does not depend on resolving this.

  5. added a commit that references this issue on Sep 21, 2026
    1526d36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    • Status
      Done ✅

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions