Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@lambdageek
Copy link
Member

@lambdageek lambdageek commented Jun 27, 2018

  1. Switch to GC Safe thread state in mono_coop_cond_signal and mono_coop_cond_broadcast because at least on Linux/glibc the underlying implementation can block on a futex (specifically in __condvar_quiesce_and_switch_g1 in glibc/ntpl/pthread_cond_common.c). If you'd like details:
  2. make mono_pmip work again under coop and hybrid suspend inside GDB/LLDB (it used to call mono_method_full_name which would try to switch GC Unsafe thread state and then assert if the developer unwittingly called it from a thread that isn't currently attached).

`mono_pmip` calls `get_method_from_ip` which needs to make a string with the
name of a method. It used to call `mono_method_full_name` which is a `MONO_API`
function and so tries to switch to GC Unsafe mode (which requires the thread to
be attached).

However `mono_pmip` is often used from GDB/LLDB and it might be invoked by the
developer from a stopped thread that isn't currently attached which would assert.

Instead call `mono_method_get_name_full` in `get_method_from_ip` which doesn't
switch the thread state.
@lambdageek lambdageek mentioned this pull request Jun 27, 2018
23 tasks
@jaykrell
Copy link
Contributor

Does this fix Alpine and is it a better fix for the thread6/7 hang at shutdown? (see f3319f7)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modern style is one space after periods.

Copy link
Contributor

@jaykrell jaykrell Jun 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this seems reasonable even with less commentary -- "external functions can block" -- klnda just a blanket/blind good assumption. But ok.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We ought to know better, but I think there's an assumption that some of these pthread operations are primitive and will behave atomically. But yea we should just assume all external calls can block.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it isn't likely, it could be they haven't been called in a while, are paged out, and just calling them requires paging them in. Just crossing from one page of code to another, without even a call, can page fault and be slow. But we can't do much about that, except maybe page-align functions and keep them under page in size, limiting the fault to their start.

@lambdageek
Copy link
Member Author

lambdageek commented Jun 27, 2018

@jaykrell I don't know what "Alpine" or thread6/7 is referring to.

I'm not sure that this PR has anything to do with f3319f7 - it doesn't look like there's any cond_signal or cond_broadcast near there.

@lewurm
Copy link
Contributor

lewurm commented Jun 27, 2018

it used to call mono_method_full_name which would try to switch GC Unsafe thread state

I call mono_method_full_name quite often during debugging sessions. Does that mean with COOP I shouldn't use it (or only carefully)? Does that apply to every function annotated with MONO_API?

…ch to GC Safe

Under Linux/glibc `pthread_cond_signal` and `pthread_cond_broadcast` can block
on a futex in `__cond_quiesce_and_switch_g1`, so switch the thread to GC Safe
mode around the blocking operation.
@lambdageek
Copy link
Member Author

@lewurm As long as you're calling it from a thread that's attached to the runtime, it's fine. I accidentally called it from some random thread - a GC worker or something libtest.c created.

@jaykrell
Copy link
Contributor

Oh I guess nevermind, it's a sem_timedwait here:

f3319f7#diff-ce0350ac89da817c904c38a837fc7b23L391

This still needs attention but it isn't related to your PR.

Maybe the fix is around here, to not skip aborting when abort already requested..
https://github.com/mono/mono/blob/master/mono/metadata/threads.c#L2482

…houldn't swith to GC Safe

On Win32, still do the thread state change here; on other platforms
its handled in the implementation of mono_w32handle_signal_and_wait
@lambdageek lambdageek requested a review from luhenry as a code owner June 28, 2018 00:55
@lambdageek
Copy link
Member Author

@monojenkins build failed

@lambdageek lambdageek merged commit ebc98c0 into mono:master Jun 29, 2018
alexanderkyte pushed a commit to alexanderkyte/mono that referenced this pull request Jul 2, 2018
…ono#9347)

* [debug] mono_pmip shouldn't assume it's called from an attached thread

`mono_pmip` calls `get_method_from_ip` which needs to make a string with the
name of a method. It used to call `mono_method_full_name` which is a `MONO_API`
function and so tries to switch to GC Unsafe mode (which requires the thread to
be attached).

However `mono_pmip` is often used from GDB/LLDB and it might be invoked by the
developer from a stopped thread that isn't currently attached which would assert.

Instead call `mono_method_get_name_full` in `get_method_from_ip` which doesn't
switch the thread state.

* [coop] mono_coop_cond_signal and mono_coop_cond_broadcast should switch to GC Safe

Under Linux/glibc `pthread_cond_signal` and `pthread_cond_broadcast` can block
on a futex in `__cond_quiesce_and_switch_g1`, so switch the thread to GC Safe
mode around the blocking operation.

* [coop] ves_icall_System_Threading_WaitHandle_SignalAndWait_Internal shouldn't swith to GC Safe

On Win32, still do the thread state change here; on other platforms
its handled in the implementation of mono_w32handle_signal_and_wait
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…ono/mono#9347)

* [debug] mono_pmip shouldn't assume it's called from an attached thread

`mono_pmip` calls `get_method_from_ip` which needs to make a string with the
name of a method. It used to call `mono_method_full_name` which is a `MONO_API`
function and so tries to switch to GC Unsafe mode (which requires the thread to
be attached).

However `mono_pmip` is often used from GDB/LLDB and it might be invoked by the
developer from a stopped thread that isn't currently attached which would assert.

Instead call `mono_method_get_name_full` in `get_method_from_ip` which doesn't
switch the thread state.

* [coop] mono_coop_cond_signal and mono_coop_cond_broadcast should switch to GC Safe

Under Linux/glibc `pthread_cond_signal` and `pthread_cond_broadcast` can block
on a futex in `__cond_quiesce_and_switch_g1`, so switch the thread to GC Safe
mode around the blocking operation.

* [coop] ves_icall_System_Threading_WaitHandle_SignalAndWait_Internal shouldn't swith to GC Safe

On Win32, still do the thread state change here; on other platforms
its handled in the implementation of mono_w32handle_signal_and_wait


Commit migrated from mono/mono@ebc98c0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants