Skip to content

feat(anr): [Non-Fatal ANR 1] Add HangManager - #6722

Open
markushi wants to merge 17 commits into
markushi/feat/non-fatal-anr-collectionfrom
markushi/feat/non-fatal-anr-1-hang-recorder
Open

markushi wants to merge 17 commits into
markushi/feat/non-fatal-anr-collectionfrom
markushi/feat/non-fatal-anr-1-hang-recorder

Conversation

@markushi

@markushi markushi commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Foundation for Non-Fatal ANRs:

  • Adds the central HangManager and the central loop, including all required interfaces
  • The HangRecorder samples the collectors on the manager's 66 ms tick thread, and the manager gives the HangRecord to the subscribers that asked for it.
  • Not wired yet (see stacked PRs).

Design

image

See full spec for more details: Non-Fatal ANR Architecture (JAVA-777)

Example Event: https://sentry-sdks.sentry.io/issues/7865095622/events/6d88b353646b4c2d884a2180f83190d0/

image

⚠️ Merge this PR using a merge commit (not squash). Only the collection branch is squash-merged into main.

#skip-changelog

@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 20, 2026
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit ca9d859.

Main concerns: public contract, host performance, data sent to Sentry.

Rules that block low also fired:

  • R4: a public API record changed: sentry-android-core/api/sentry-android-core.api

This label is a hint for reviewers, not an approval.

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against ca9d859

@sentry

sentry Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.65.0 (1) release

⚙️ sentry-android Build Distribution Settings

@markushi
markushi marked this pull request as ready for review November 20, 2026 18:01
@markushi
markushi requested review from a team and adinauer as code owners November 20, 2026 18:01
@markushi
markushi requested review from 0xadam-brown and romtsn and removed request for a team November 20, 2026 18:01
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 20, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@markushi
markushi marked this pull request as draft November 23, 2026 07:51
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 23, 2026
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 23, 2026
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 24, 2026
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 24, 2026
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 24, 2026
@markushi
markushi marked this pull request as ready for review November 24, 2026 12:40
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 24, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want higher recall? High effort reviews run extra passes and find more bugs. A team admin can switch effort levels in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit c7b7e2c. Configure here.

@markushi
markushi removed this pull request from stack #6730 November 24, 2026 13:01
@markushi
markushi added this pull request to stack #6753 November 24, 2026 13:02
@linear-code

linear-code Bot commented Nov 24, 2026

Copy link
Copy Markdown

JAVA-475

@github-actions github-actions Bot removed the risk: medium PR risk score: medium label Nov 25, 2026
@markushi
markushi removed this pull request from stack #6753 November 25, 2026 09:41
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 25, 2026
@markushi
markushi added this pull request to stack #6761 November 25, 2026 09:41
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 27, 2026
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 27, 2026

@runningcode runningcode left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

starting to go through this, just leaving some early comments and I'll get back to reviewing tomorrow!

}

@SuppressWarnings("ReferenceEquality")
private void postOnManagerThread(final @NotNull Runnable task) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I see these types of methods, i always wonder if it could cause out of order execution and if that matters.
In this case, my clanker tells me that it only ever goes down the first branch so to simplify the code we could remove this method entirely.

// Runs before any other task of this single-thread executor.
current.execute(() -> executorThread = Thread.currentThread());
}
current.execute(() -> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we wrap the execution of the task in a try/catch ?
otherwise my clanker tells me that the ScheduledThreadPoolExecutor swallows the exception and there's no way of knowing the task failed and could leave us in a bad state.

private final @NotNull MonotonicTicker ticker;
private final @NotNull EpochClock epochClock;
private final long pollingIntervalMs;
private final long maxRecordingTimeMs;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my clanker tells me that these maxRecordingTimeMs and maxSamples are essentially the same limit since we're sampling the app at a fixed rate.
it could be confusing to users which one to adjust when their samples stop if they aren't aware there are two different limits. could we consolidate?

@sentry-junior sentry-junior Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The manager-thread model is clean and the tests are thorough. Two things I'd like settled before this lands, plus a +1 on the open thread about task exceptions:

  • HangManager ignores which signal opened the hang. signalListener throws away the signal argument, so in handleSignalChanged an IDLE from any signal ends the open hang, and in handleSignalCanceled a cancel from any signal drops it. With two signals, signal B going IDLE or canceling while signal A is still SUSPICIOUS truncates or discards A's hang. A won't re-report SUSPICIOUS because it only reports changes, so the rest of that hang is lost. The test second suspicious from another signal does not start a second record locks in this behavior: A's IDLE ends the record while the other signal is still suspicious. Either track the signal that opened the hang and end or cancel only on its events, or take a single HangSignal if more than one isn't a real use case. Right now the List<HangSignal> API promises more than the state machine supports.
  • HangRecorder.start clears the stores at the start of every persisting hang. On the next launch, main-thread hangs during startup are common. If one starts before the fatal ANR path has read the previous process's samples, those samples are wiped. Can you point to where the ordering is guaranteed (read before the first subscribe, or a store per process run)? If it lives in a later PR in the stack, a short note in the HangManager Javadoc would help.

The failing Build Job ubuntu-latest is DistrustedCertificatesTest in :sentry, which this PR doesn't touch.

listener.onMetronomeTick(nowMs);
}
final @Nullable HangRecorder recorder = activeRecorder;
if (recorder != null && nowMs - recorder.getStartTimeMs() < maxRecordingTimeMs) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is effectively the same thing as Deadline. Can we use that abstraction here instead of manually doing math?

@runningcode runningcode left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the junior post there, i was asking it about this PR and it decided to post its review here.

My clanker proposed this alternative design in order to remove the Metronome and simplify the code. That would fix:

  • this escape
  • Metronome interfaces removed
  • my comment about the postOnManagerThread
  • all the listener wiring

the proposed design that my clanker came up with is that the manager asks on every tick and the signal just answers.

what do you think of that approach? it says the trade off is that it assumes the signals are polled on a tick which might not be the case for future use cases of this but I always say YAGNI.

My clanker gave me some code samples for this approach that I can share. Happy to have a call to discuss it!

}

/**
* The last unsubscribe closes the manager: it stops the signals and the tick, drops an open hang

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is somewhat unexpected here since usually the principle is "whatever creates it, closes it" so that the lifecycle is clear.

this.maxRecordingTimeMs = maxRecordingTimeMs;
this.maxSamples = maxSamples;
this.executorFactory = executorFactory;
this.signals = Collections.unmodifiableList(new ArrayList<>(signalFactory.create(this)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a general design pattern thing: its not a good idea to pass (this) in the constructor because then another object may see this object before it is crated which breaks the guarantees that final gives us.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants