Bug 26846 - RNGCryptoServiceProvider.GetBytes may hang on unloading application domain
Summary: RNGCryptoServiceProvider.GetBytes may hang on unloading application domain
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib (show other bugs)
Version: 3.12.0
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-02-08 13:31 UTC by Leonid Shalupov
Modified: 2015-02-08 13:31 UTC (History)
1 user (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description Leonid Shalupov 2015-02-08 13:31:42 UTC
We have some code in finalizers that call Guid.NewGuid, which calls RNGCryptoServiceProvider.GetBytes for guid generation.
Sometimes that code hangs on appdomain unload with the following stacktrace from gdb:

#0  0x00007f1b28578dbd in read () at ../sysdeps/unix/syscall-template.S:81
#1  0x00000000005832d9 in read (nbytes=14, buf=0x7f1b271fe382, __fd=0) at /usr/include/x86_64-linux-gnu/bits/unistd.h:44
#2  ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngGetBytes (handle=0x0, arry=<optimized out>) at rand.c:295
#3  0x0000000041fcb238 in ?? ()
#4  0x00007f1b271fe168 in ?? ()
...

Note that "read" call reads from fd 0 (stdin) which leads to hang.

I found only one case when it's possible:
handle is zeroed in RNGCryptoServiceProvider's destructor
https://github.com/mono/mono/blob/master/mcs/class/corlib/System.Security.Cryptography/RNGCryptoServiceProvider.cs#L145

The following patch was successfully (but not correctly) fixed it: https://github.com/JetBrains/mono/commit/f6d77139e76c4f76ddef954411892367d82795f4

On my codebase the bug is reproduced consistently, but I was unable to extract smaller sample.
I'm ready to test the solution from Mono at any time.

Note You need to log in before you can comment on or make changes to this bug.