fix(nft): the per-batch markersAdded() call is not guarded against re-entry

Open
#130 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
cpp
Domain
backend

Research direction

Start with NFTDetector.h and the per-batch markersAdded() call at the end of addNFTMarkers(); compare it with the guarded calls in ensureDetector() and the creation backfill. Then add tests for a second batch whose callback calls teardown() and addNFTMarkers(). Decide and document the outcome for already committed markers, and verify that nested calls are refused and the detector remains alive until its callback returns.

Written by the indexing model from the issue text.

Description

NFTDetector.h says that a call back into the core from markersAdded() is refused before it changes anything, and that the outer addNFTMarkers() / setupAR2() fails. That holds for the calls made from ensureDetector(), the backfill at creation, which runs under CreatingGuard. But the per-batch call at the end of addNFTMarkers() runs outside the guard:

if (batchSize > 0 && this->detector) {
  this->detector->markersAdded(markerIds, datasetPathnames);
}

From there, teardown(), setupAR2() or setCamera() is not refused. It destroys the detector while its markersAdded() is still running, which is undefined behaviour. addNFTMarkers() recurses.

Found by Qodo on #129. Introduced by #116; #124 added the guard for the creation path only. It only shows with a detector that breaks the documented contract, so it does not block 0.13.0.

To decide

When the per-batch call runs, the batch is already committed: ids are assigned and the KPM reference data is set. Returning {} at that point would report a failure for markers that are loaded. Options:

  • refuse the nested call, discard the detector, log, and still return the ids, so the next setupAR2() recreates the detector; update NFTDetector.h to say so;
  • or notify before committing, and roll back if the detector re-entered.

Fix sketch

Share the RAII guard of ensureDetector() with the per-batch call. Afterwards, check creationReentered and whether the detector or the handles changed.

Test

A second batch whose markersAdded() calls teardown(), and separately addNFTMarkers(). The nested call must be refused, the detector must stay alive until its callback returns, and the outcome must match the chosen contract.

Dominant language
C++
Stars
21
Forks
8
Avg merge
3h 42m
Merged PRs (30d)
22

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from webarkit/WebARKitLib

All issues in webarkit/WebARKitLib

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.