Skip to content

Embedded Mono hangs when using native threads #16192

Closed

Description

@krixkrix

Embedded mono hangs when using native threads to call into a mono function that allocates semi-large chunks of memory. Fails for mono 5.20.1.34 and 6.0.0.319, but works for 5.18.0.268.

Steps to Reproduce

A full demo example is provided in repo https://github.com/krixkrix/mono_thread_problem.
The demo was tested on linux.

The demo contains a simple C# function that allocates a memory block.

      public void Foo()
        {
            for (int i = 1; i<= 10; i++)
            {
                Console.WriteLine(String.Format("Allocate {0} of 10", i));
                byte[] pixels = new byte[1000000];  // size affects how soon the thread hangs
            }
            Console.WriteLine("Done");
            return;
        }

This function is called from managed code (c++) in different threads, but each call completes before the next.

Current Behavior

The output of the test project is:

This should call Foo a hundred times, but hangs...
call Foo 0
Allocate 1 of 10
Allocate 2 of 10
Allocate 3 of 10
Allocate 4 of 10
Allocate 5 of 10
Allocate 6 of 10
Allocate 7 of 10
Allocate 8 of 10
Allocate 9 of 10
Allocate 10 of 10
Done
call Foo 1
Allocate 1 of 10
Allocate 2 of 10
Allocate 3 of 10
Allocate 4 of 10
Allocate 5 of 10
Allocate 6 of 10
Allocate 7 of 10
Allocate 8 of 10

  • When called from different threads it will hang after some time.
  • When called from a single thread it can go on forever.
  • I notice that the size of memory allocation in the c# code matters.

This may relate to #14725

On which platforms did you notice this

[ ] macOS
[x ] Linux, Ubuntu 18.04
[ ] Windows

Version Used:

Fails with:

  • mono 6.0.0.319
  • mono 5.20.1.34

Works with:

  • mono 5.18.0.268

c++ code:
g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0

Activity

  1. lewurm commented on Aug 19, 2019

    @lewurm
    Contributor

    does running with MONO_THREADS_SUSPEND=preemptive fixes your issue? we switched to hybrid recently.

  2. krixkrix commented on Aug 22, 2019

    @krixkrix
    Author

    Yes, actually it does.
    So what does this mean, is it recommended or deprecated to do it this way?

  3. lewurm commented on Aug 23, 2019

    @lewurm
    Contributor

    @krixkrix thanks for verifying. It means it's an issue in our new thread suspension mechanism. For now you can use MONO_THREADS_SUSPEND=preemptive as a workaround, in the long-term we have to fix this.

    /cc @lambdageek

  4. krixkrix commented on Aug 26, 2019

    @krixkrix
    Author

    So is MONO_THREADS_SUSPEND=preemptive supported and tested, so it's safe to use or should I stick with mono 5.18 which works for me so far?

  5. jaykrell commented on Aug 26, 2019

    @jaykrell
    Contributor

    Yes. MONO_THREADS_SUSPEND=preemptive makes it work like old releases.
    It is not deprecated.

  6. thaystg commented on Sep 16, 2019

    @thaystg
    Contributor

    Using MONO_THREADS_SUSPEND=hybrid or coop we get the hang.

  7. added a commit that references this issue on Sep 18, 2019
    d256da9
  8. added a commit that references this issue on Sep 25, 2019
    5b108b5
  9. added a commit that references this issue on Jan 20, 2020
    1085a39
  10. Evengard commented on Feb 12, 2023

    @Evengard

    It seems I'm still having this problem. Mono is embedded into uWSGI, and the env var MONO_THREADS_SUSPEND=preemptive does help it to get through, without it it just hangs forever.
    The app was compiled in a 6.8.0 env and running in a 6.12.0 env.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions