Skip to content

Commit afa86ee

Browse files
fix(audience-sdk): log instead of swallow on consumer callbacks; honour CancellationToken (SDK-256)
- Three bare catch { } blocks (NotifyErrorCallback, ParseRejectedCount, NotifyError) previously absorbed every exception type, including IL2CPP MissingMethodException / TypeLoadException from stripping. Replace each with catch (Exception ex) { Log.Warn(...) } so the failure surfaces in test logs without crashing the SDK. - ParseRejectedCount accepts and honours a CancellationToken propagated from SendBatchAsync, so a stalled HTTP body read can be cancelled instead of holding the cell open until the 60-min timeout.
1 parent 8d8fa8d commit afa86ee

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

src/Packages/Audience/Runtime/ImmutableAudience.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,9 @@ private static void NotifyErrorCallback(Action<AudienceError>? onError, Audience
426426
{
427427
onError(new AudienceError(code, message));
428428
}
429-
catch
429+
catch (Exception ex)
430430
{
431-
// Swallow: a buggy OnError must not crash the SDK.
431+
Log.Warn($"onError threw {ex.GetType().Name}: {ex.Message}");
432432
}
433433
}
434434

src/Packages/Audience/Runtime/Transport/HttpTransport.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ internal async Task<bool> SendBatchAsync(CancellationToken ct = default)
105105
// rejected; if any, tell the studio via onError. Rejected
106106
// messages are validation failures — retrying won't help,
107107
// so the batch is deleted either way.
108-
var rejected = await ParseRejectedCount(response).ConfigureAwait(false);
108+
var rejected = await ParseRejectedCount(response, ct).ConfigureAwait(false);
109109
_store.Delete(batch);
110110
ResetBackoff();
111111
if (rejected > 0)
@@ -275,15 +275,20 @@ private void ResetBackoff()
275275
// Reads the response body and pulls out the "rejected" count. Returns
276276
// 0 if the body is missing or unreadable — the body is only for
277277
// reporting, so failing to read it must not break the success path.
278-
private static async Task<int> ParseRejectedCount(HttpResponseMessage response)
278+
private static async Task<int> ParseRejectedCount(HttpResponseMessage response, CancellationToken ct = default)
279279
{
280280
string body;
281281
try
282282
{
283+
// .NET Standard 2.1 (Unity's apiCompatibilityLevel 6) only exposes
284+
// the parameterless ReadAsStringAsync; the (CancellationToken)
285+
// overload is .NET 5+. Observe the token at the call boundary.
286+
ct.ThrowIfCancellationRequested();
283287
body = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
284288
}
285-
catch
289+
catch (Exception ex)
286290
{
291+
Log.Warn($"ParseRejectedCount threw {ex.GetType().Name}: {ex.Message}");
287292
return 0;
288293
}
289294
if (string.IsNullOrEmpty(body)) return 0;
@@ -332,10 +337,9 @@ private void NotifyError(AudienceErrorCode code, string message)
332337
{
333338
_onError(new AudienceError(code, message));
334339
}
335-
catch
340+
catch (Exception ex)
336341
{
337-
// Consumer callback threw. Swallow: the SDK must not surface
338-
// exceptions through the error-reporting path itself.
342+
Log.Warn($"_onError threw {ex.GetType().Name}: {ex.Message}");
339343
}
340344
}
341345
}

0 commit comments

Comments
 (0)