Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,46 @@ public void Exceptions_Are_Thrown_By_DeSerializer()
// }
// }

/// <summary>
/// Test Serializer disposal when the object is uninitialized but finalizer still runs
/// This is not the exact usage scenario but simulates the case when unknown garbage collection race condition
/// causes the finalizer to run when the serializer object is null
/// </summary>
[Test]
public void SerializerConstructorDoubleFinalizerTest()
{
// Create uninitialized object to simulate failed constructor leaving _serializer null
var serializer = (GlueSchemaRegistrySerializer)System.Runtime.Serialization.FormatterServices
.GetUninitializedObject(typeof(GlueSchemaRegistrySerializer));

// Manually trigger finalizer on this uninitialized object
var finalize = typeof(GlueSchemaRegistrySerializer).GetMethod("Finalize",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
finalize?.Invoke(serializer, null);

Assert.Pass("Disposal failure handled gracefully");
}

/// <summary>
/// Test Deserializer disposal when the object is uninitialized but finalizer still runs
/// This is not the exact usage scenario but simulates the case when unknown garbage collection race condition
/// causes the finalizer to run when the deserializer object is null
/// </summary>
[Test]
public void DeserializerConstructorDoubleFinalizerTest()
{
// Create uninitialized object to simulate failed constructor leaving _deserializer null
var deserializer = (GlueSchemaRegistryDeserializer)System.Runtime.Serialization.FormatterServices
.GetUninitializedObject(typeof(GlueSchemaRegistryDeserializer));

// Manually trigger finalizer on this uninitialized object
var finalize = typeof(GlueSchemaRegistryDeserializer).GetMethod("Finalize",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
finalize?.Invoke(deserializer, null);

Assert.Pass("Disposal failure handled gracefully");
}

private static GenericRecord GetTestAvroRecord()
{
var recordSchema = Schema.Parse(TestAvroSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private static void Validate(byte[] encoded)

private void ReleaseUnmanagedResources()
{
_deserializer.Dispose();
_deserializer?.Dispose();
}

private void Dispose(bool disposing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private static void Validate(GlueSchemaRegistrySchema schema, byte[] bytes)

private void ReleaseUnmanagedResources()
{
_serializer.Dispose();
_serializer?.Dispose();
}

private void Dispose(bool disposing)
Expand Down
Loading