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
Thread safety issue with XResourceLoader #968
Comments
I was under the impression that robolectric runs on a single thread. |
Robolectric runs a single thread however the application that it launches is free to spawn its own threads. Robolectric initializes its resource classes on-demand. That is, the first attempt, by the app, to read a resource, will trigger the initialization code above. That is what happened in my case. 2 threads were both triggering this initialization thus causing a race condition that lead to frequent failures. So given that the class above has restrictions on how it can be used, it would be beneficial to enforce this in code (i.e. synchronize the method) rather than assuming that it will only be used in a certain manner. |
@erd - It is not clear to me why this was closed. I did not see any comments on the matter. The diffs in the commit appeared to be in an entirely unrelated area. I'm new to GitHub, so perhaps I am missing something. Could you please elaborate on why the issue was closed and if a fix was implemented? |
It was merged manually. When pushed to GitHub the PR was automatically
|
@JakeWharton - So are you saying that a fix was committed? I checked what I believe is the latest version of the file in question, org.robolectric.res.XResourceLoader.java, and I see no changes. The threading issue is still present. Could you please elaborate. |
The change should be here:
|
@JakeWharton - The changes in the provided link are completely unrelated to the issue I entered. So I ask again: Did someone actually fix the issue I entered: yes or no? |
Dunno. I've been replying from my phone. If that SHA doesn't match your
|
@erd et al. I do not have authorization to re-open this. Could someone please take care of that for me? Thanks. |
@eric-kampf Yes, I typoed the issue number in a different commit. My apologies. |
Any plans to fix this? It's a very obvious bug with a simple fix. |
I believe #1083 could be related to this. I'm able to consistently repro something similiar in 2 environments:
I tried to get a 3.0-SNAPSHOT build running locally to implement the fix described by @eric-kampf but I am running into some issues setting up my local maven repo to resolve dependencies. OSX environment seems to run tests (edit: about 50% failure rate), ubuntu misses 90% of the time. |
I'm trying to work around this issue, besides modifying the code to synchronize that piece, has anyone found a way to resolve it? I'm seeing the issue appear intermittently in OSX, and recurrently on a Ubuntu environment. |
Resource loading now happens before the test starts, so this issue is resolved. |
I have experienced a threading issue with org.robolectric.res.XResourceLoader. Observe the initialize method:
Note that the "isInitialized" flag, which appears to prevent multiple initializations, does not provide full protection. It is possible that 2 or more threads could call the "doInitialize()" method. In fact, I have seen that in my Android application. When this occurs, bad things happen (concurrent modification of data structures) that break the Robolectric test.
My recommendation is that the "initialize" method of this class be synchronized.
The text was updated successfully, but these errors were encountered: