Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should use THREADCHECK instead of ASSERT_R_THREAD() #7

Closed
wch opened this issue Nov 16, 2018 · 5 comments
Closed

Should use THREADCHECK instead of ASSERT_R_THREAD() #7

wch opened this issue Nov 16, 2018 · 5 comments

Comments

@wch
Copy link
Owner

wch commented Nov 16, 2018

As of
wch/r-source@3c59cea, R itself has a THREADCHECK macro to make sure that various functions are called from the correct thread.

Commit wch/r-source@5cf1f4b also added the checks that were in ASSERT_R_THREAD().

The assertthread build should be changed to use the built-in checks.

@jeroen
Copy link
Contributor

jeroen commented Nov 16, 2018

Cool! Do these macro's prevent the GC from running in a non-main thread?

@wch
Copy link
Owner Author

wch commented Nov 16, 2018

Yes, I believe that if it tries to do GC in a thread, it calls R_suicide(), which I assume kills the R process. :)

The first link above was previously incorrect, but I've fixed it, and if you follow it you can see how it works. Just curious: in your work, have you had problems with GC in other threads?

@jeroen
Copy link
Contributor

jeroen commented Nov 16, 2018

I avoid calling the R api in threads because afaik none of the api's are thread-safe due to the random GC runs. I was hoping these changes could help with suspending the GC in threads, but I guess that is not the purpose here :)

@wch
Copy link
Owner Author

wch commented Nov 29, 2018

Fixed in e9899c3.

@wch wch closed this as completed Nov 29, 2018
@wch
Copy link
Owner Author

wch commented Nov 29, 2018

@jeroen I think the problem is even worse than having GC events run in the wrong thread. If you do any R memory management at all in a different thread, there's the possibility of memory corruption due to races. For example, if you look at wch/r-source@5cf1f4b, you can see that protect() and unprotect() manipulate R_PPStack, and similarly, R_PreserveObject() and R_ReleaseObject() manipulate R_PreciousList. In the past I have run into many weird, hard-to-reproduce bugs because of these races.

But I agree about the overall lesson: don't call the R API from a different thread.

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

No branches or pull requests

2 participants