-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
Summary
The method CloudEventEnvelopeExtensions.Serialize temporarily sets envelope.CloudEvent = null to serialize the rest of the envelope and then restores it. This is not thread-safe and is not exception-safe.
Context
- File:
src/Events/Extensions/CloudEventEnvelopeExtensions.cs - Method:
Altinn.Platform.Events.Extensions.CloudEventEnvelopeExtensions.Serialize(CloudEventEnvelope envelope)
Problem
- Temporary mutation of
envelope.CloudEventintroduces a data race: concurrent callers may observeCloudEventasnull. - If
JsonSerializer.Serializethrows,envelope.CloudEventmay remainnull(no try/finally), leaving the object in an inconsistent state.
Impact
- Intermittent
NullReferenceExceptions or incorrect behavior under concurrent usage. - Difficult-to-diagnose flakiness if the same envelope instance is reused.
Proposed fixes (either approach is fine)
- Preferred: Avoid mutating the input. Compose the JSON output via
JsonObject/JsonNode(or serialize a shallow copy that excludesCloudEvent), then insert the serialized CloudEvent payload (cloudEvent.Serialize()) into the final JSON. - Minimal: If we must keep the current approach, wrap the mutation around the serialize call in a try/finally to guarantee restoration even if serialization throws.
Acceptance criteria
Serializeno longer mutates shared state during serialization, or mutation is fully exception-safe (try/finally).- Unit tests:
Serializeleavesenvelope.CloudEventunchanged after success and after a forced serialization exception.- Optional: a simple parallel invocation smoke test that ensures
CloudEventis not observed asnull(or document thread-safety expectations explicitly).
References
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Status
π New