Bugfix :: Fix F# exception serialization to preserve fields#19342
Conversation
❗ Release notes required
|
c9b17c0 to
f6f7424
Compare
|
/azp run fsharp-ci |
|
Azure Pipelines successfully started running 1 pipeline(s). |
675d541 to
4b0a72e
Compare
|
An edge case, but now this causes a runtime crash due to field name collision with open System
open System.Runtime.Serialization
exception Foo of Message:string
let e = Foo("hello")
let info = SerializationInfo(e.GetType(), FormatterConverter())
let ctx = StreamingContext(StreamingContextStates.All)
e.GetObjectData(info, ctx) |
1b2f39a to
2debec9
Compare
|
Good catch! Fixed in 234f944 — serialization keys now use the backing field name (e.g. Added a dedicated test ( |
|
/azp run fsharp-ci |
|
Azure Pipelines successfully started running 1 pipeline(s). |
abonie
left a comment
There was a problem hiding this comment.
What happens if exception is serialized with an older compiler and deserialized with one that has these changes? If it's not going to work then perhaps this should be marked as a breaking change? Or not because it wasn't working before either?
773eba0 to
d485a32
Compare
Fixes #878 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Changed eventsWhen condition in 'Cancel running jobs with the same key' test to wait until all 10 outdated jobs are canceled and the new job finishes, instead of stopping at the first Finished event. This prevents race conditions where assertions run before all cancellations complete. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…issue #18411) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use IL backing field name (with @ suffix) as the serialization key instead of the property name. This prevents duplicate key crashes when an F# exception field name collides with a key already used by Exception.GetObjectData (e.g., 'exception Foo of Message:string'). The backing field name (e.g., 'Message@', 'Data0@') is guaranteed to not collide with base Exception serialization keys. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add Issue_878_ExceptionSerialization_MessageFieldCollision test that reproduces the exact edge case where an F# exception field named 'Message' collides with Exception.GetObjectData's 'Message' key. The test verifies: - GetObjectData does not throw with duplicate key - F# field is serialized under backing field name 'Message@' - Base Exception's 'Message' key remains accessible - Full serialization roundtrip preserves the field value Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Eliminate ilBaseOnlyCtorInstrs/ilBaseOnlyCtor block that duplicated ilInstrsForSerialization/ilCtorDefForSerialization by introducing shouldRestoreFields flag - Simplify 3-branch if/elif/else to 2-branch conditional - Remove unused ilPropName parameter from emitSerializationFieldIL callback Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…yIL list Replace the duplicated getActualIL helper and Assert.Contains call with a proper IL fragment in the verifyIL list, verifying the AddValue callvirt instruction is emitted in the GetObjectData method. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…rt unrelated AsyncMemoize fix - Add [CompilerGenerated] and [DebuggerNonUserCode] attributes to the generated serialization constructor and GetObjectData override on exception types, matching the pattern used by other compiler-generated methods in IlxGen. - Update verifyIL test to check for the new attributes. - Update all EmittedIL baselines (Nullness, SerializableAttribute) for both netcore and net472, debug and release configurations. - Revert unrelated AsyncMemoize flaky test fix (not part of this PR scope). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
d485a32 to
bb46647
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Warning The 🤖 LabelOps — Conflict Resolution. Merged
✅ Verified: compiler builds, SerializableAttribute tests (4) pass, exception regression tests (36) pass.
|
Resolve 8 modify/delete conflicts: delete old-style .debug.bsl/.release.bsl files superseded by unified baselines from PR #19611. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
After merging main (which eliminated debug/release baseline duality in PR #19611), regenerate the unified .netcore.bsl baselines to include the GetObjectData overrides and field-restoring deserialization constructors from this PR. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…FieldSerializationSupport (F# 11) The GetObjectData override and field-restoring deserialization constructor for exception types are now gated behind langversion 11. With langversion <=10 (the current default), exceptions emit only the base-call ctor (status quo ante PR #19342). - Add LanguageFeature.ExceptionFieldSerializationSupport, mapped to F# 11.0 - Gate shouldRestoreFields and GetObjectData emission on the feature - Update tests to use withLangVersion "11" - Update all IL baselines (SerializableAttribute, Nullness) to reflect the default-langversion output (no field serde IL) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…FieldSerializationSupport (F# 11) The GetObjectData override and field-restoring deserialization constructor for exception types are now gated behind langversion 11. With langversion <=10 (the current default), exceptions emit only the base-call ctor (status quo ante PR #19342). - Add LanguageFeature.ExceptionFieldSerializationSupport, mapped to F# 11.0 - Gate shouldRestoreFields and GetObjectData emission on the feature - Update tests to use withLangVersion "11" - Update all IL baselines (SerializableAttribute, Nullness) to reflect the default-langversion output (no field serde IL) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…FieldSerializationSupport (F# 11) The GetObjectData override and field-restoring deserialization constructor for exception types are now gated behind langversion 11. With langversion <=10 (the current default), exceptions emit only the base-call ctor (status quo ante PR #19342). - Add LanguageFeature.ExceptionFieldSerializationSupport, mapped to F# 11.0 - Gate shouldRestoreFields and GetObjectData emission on the feature - Update tests to use withLangVersion "11" - Update all IL baselines (SerializableAttribute, Nullness) to reflect the default-langversion output (no field serde IL) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Fixes F# exceptions losing their field values during serialization.
Exceptions with fields now emit a
GetObjectDataoverride and a deserialization constructor that save/restore the fields viaSerializationInfo, enabling correctISerializableroundtrips.Note: This fix is primarily relevant for users targeting .NET Framework 4.x or .NET Core ≤7, where
BinaryFormatter/ISerializableserialization is still available. On .NET 8+,StreamingContextis removed and the serialization members are not emitted. The entireISerializableinfrastructure is deprecated (SYSLIB0051) — no modern serializer (System.Text.Json,DataContractSerializer, etc.) usesGetObjectData.FSharp.Core exception caveat: FSharp.Core has
[assembly: SecurityTransparent], which on .NET Framework prevents overriding theSecurityCriticalmethodException.GetObjectData. FSharp.Core exceptions (e.g.MatchFailureException) therefore get only the base-call deserialization constructor (status quo) but not theGetObjectDataoverride. This is tested explicitly. User-defined exceptions are unaffected — they get full serialization support.