Skip to content

[AIPROFVIS-516] [AIPROFVIS-762] Fail the job instead of terminating when an exception is thrown, and handle failed memory pool allocations - #1150

Draft
dhingora-amd wants to merge 12 commits into
mainfrom
dhingora/fix-job-exceptions
Draft

dhingora-amd wants to merge 12 commits into
mainfrom
dhingora/fix-job-exceptions

Conversation

@dhingora-amd

@dhingora-amd dhingora-amd commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

An exception thrown inside a controller job (for example std::bad_alloc from the pool allocator) escaped its worker thread and terminated the app. Catching it alone isn't enough: an exception partway through a track or graph fetch left its segments marked as being fetched, so later fetches waited forever.

Technical Details

  • CallCatchingExceptions turns exceptions into failed results. Job::Execute uses it, and so do the track and graph fetch code, so fetch marks and model slices are always cleaned up.
  • MemoryManager::Allocate returns nullptr instead of throwing, and the graph's level-of-detail code now handles that nullptr.
  • New tests cover throwing jobs. Temporary fault injection (now removed) confirmed no crashes or hangs.

This PR now includes the changes described here: #1217

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Security scan found no critical or high findings on lines changed by this pull request

View the security scan run for complete reports.

“New” means the finding is located in a changed file on an added or modified line; it is not a base-versus-head findings delta.

@tomk-amd

Copy link
Copy Markdown
Collaborator

My suspicion is that this fix will leave the track data in a "bad" state, without the view (User) being notified. Also if memory pressure is high enough that allocations are failing, I'm not sure that other parts of the system won't be falling apart too.

In any case - this is probably better than out right crashing (current situation) but potentially silently leaving holes in the data is also not good.

@tomk-amd

Copy link
Copy Markdown
Collaborator

As suspected ...

AI REVIEW

The approach is sound, but I'd like a few changes before merging. Worker threads no longer die on an exception, and segments no longer stay stuck as "being fetched". However, for the most likely out-of-memory spot, allocating events during a track fetch, the error never reaches the job result. That case now turns into silently missing data instead of a failed job. There's also a new assert crash in Debug builds on the graph path.

1. Track fetch failures are overwritten by the segment walk

In Track::FetchSegments (rocprofvis_controller_track.cpp, around lines 412–457), the fetch result is correctly used to clear the processed flags. After the loop, though, this runs:

if(result != kRocProfVisResultCancelled)
{
    result = GetSegments()->FetchSegments(start, end, user_ptr, future, func);
}

So an out-of-memory or exception result from FetchFromDataModel becomes Success whenever any resident segment overlaps the range.

  • A raw track fetch reports success while events are missing.
  • The graph path is worse. Graph::GenerateLOD gets Success back from m_track->FetchSegments, builds the level-of-detail data from the partial events, and marks those segments valid. The incomplete data stays cached until the memory manager's eviction thread removes it.

Suggested fix: keep the first non-success result across the loop and don't let the walk replace a failure, the same way it already treats cancellation.

2. Graph::Fetch asserts on the results this PR now returns

rocprofvis_controller_graph.cpp, line 752:

ROCPROFVIS_ASSERT(result == kRocProfVisResultSuccess ||
                  result == kRocProfVisResultOutOfRange ||
                  result == kRocProfVisResultCancelled);

GenerateLOD can now return kRocProfVisResultMemoryAllocError or kRocProfVisResultUnknownError. ROCPROFVIS_ASSERT wraps assert, so a Debug build aborts on the exact failure this PR is meant to survive. CI and the fault-injection run would both miss it, because they use release builds.

Suggested fix: drop the assert or allow those two result codes.

3. A failure in an earlier request or range is masked by a later success

These lines are untouched, but the PR now relies on them:

  • WaitForAndProcessTraceReadRequests (rocprofvis_controller_track.cpp, lines 615–624) sets result to each request's result in turn. A track range is split into up to two read requests. If the first fails with out-of-memory and the second succeeds, the range is reported as successful and marked valid with half its events missing.
  • Graph::GenerateLOD's loop over fetch ranges does the same. Each range's flags are cleared correctly, but only the last range's result is returned.

Before this PR an allocation failure ended the process, so these paths never had to report one.

Minor

  • Cleanup is still skipped in one case. If an exception escapes ScheduleTraceReadRequests or the request loop (for example requests.push_back throwing bad_alloc), the database futures that were already started are never freed, and they stay registered on the controller Future. This is unlikely, but the catch in FetchSegments sits too high up to clean them up.
  • Test coverage gap. The new tests cover CallCatchingExceptions and Job, but not the bug the PR actually fixes: processed flags cleared and no hang after a failed fetch. Nothing tests Allocate returning nullptr either. A test-only hook that makes MemoryManager::Allocate fail, followed by a second fetch of the same range, would cover issues 1–3.
  • Framing in the PR description. A genuine out-of-RAM condition rarely shows up as a catchable std::bad_alloc. On Linux the kernel's out-of-memory killer usually ends the process first. On Windows, by the time bad_alloc is thrown, the next allocation elsewhere will likely fail too. The bigger practical win is that a bug, such as bad trace data, an oversized resize, .at(), or stoull, now fails one job instead of the whole app. It may be worth saying that in the description.
  • Out of scope, but worth noting. The model layer runs its own std::thread workers (rocprofvis_db.cpp, rocprofvis_db_query_manager.cpp). An exception on those threads can still terminate the app.

Summary

Issues 1 and 2 are why I'm requesting changes. Issue 1 means an allocation failure on a graph fetch can leave wrong data cached and displayed with no error. Issue 2 is a crash in Debug builds. Both fixes are small and stay within the files this PR already changes. Recovering from memory pressure (evicting the cache and retrying with a cooldown) and showing the failure on the track in the View can be a follow-up.

Clean up all of the downstream code that did special handling for kRocProfVisResultOutOfRange.
A range with 1M or more events is split into two read requests. If one failed to start, the fetch still reported success and the range was marked valid with part of its events missing. The fetch now fails with kRocProfVisResultUnknownError when fewer requests start than were asked for, unless it was cancelled. The requests that did start are still processed so their slices and futures are released.
Adds the "Graph Range With Empty LOD Segment" section. It finds an LOD
segment of an event graph that covers no track segment, followed later by
one with events, and fetches the segments between them first. A fetch
across all of them is then split into an empty range and a range with
events, and must succeed and return the events.

Before empty ranges were treated as success, this fetch returned
kRocProfVisResultOutOfRange with no entries, even though the segment with
events had been generated. The section finds a layout like this in both
sample traces and fails if neither has one.
…fVisResultOutOfRange (#1217)

* Treat empty track areas as success, not kRocProfVisResultOutOfRange

Clean up all of the downstream code that did special handling for kRocProfVisResultOutOfRange.

* Fail a track fetch when only some of its read requests start

A range with 1M or more events is split into two read requests. If one failed to start, the fetch still reported success and the range was marked valid with part of its events missing. The fetch now fails with kRocProfVisResultUnknownError when fewer requests start than were asked for, unless it was cancelled. The requests that did start are still processed so their slices and futures are released.
Passing a lambda converted it to a std::function at the call site, which
can allocate for larger captures. A failed allocation threw before the
try, so the cleanup the wrapper protects was skipped, such as clearing
the processed flags on segments. The template takes the callable by
reference, so nothing can throw before the try. The logging moves into
out-of-line functions so the header doesn't need spdlog.
@tomk-amd
tomk-amd marked this pull request as draft November 27, 2026 14:27
Track::FetchSegments and Graph::GenerateLOD mark segments processed while
building their list of fetch ranges, and only clear the marks after the
fetch. If push_back threw while growing the list, the exception was now
caught, so the marks stayed set and later fetches of those segments
waited forever. Reserving room for every segment in the request up front
means push_back can't allocate.
…ot if the object constructor throws

MemoryManager::Allocate created the pool with a raw new, then inserted it into m_object_pools. If either map operation threw, the pool leaked. Hold it in a unique_ptr until m_object_pools owns it.

Allocate marks a slot as used before the object is constructed in it. If the constructor threw (SampleLOD copies its children vector), the slot stayed marked, and CleanUp later called ~Handle() on memory that was never constructed. Split the slot release out of Delete into FreeSlot, and call it without a destructor from NewEvent, NewSample and NewSampleLOD when construction throws.
…hrows and release a pool slot if the object constructor throws"): drop the try/catch it added around the placement new in NewEvent and NewSample.

Also drop the unused iterator from the emplace in Allocate.

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants