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

Avoid returning incomplete TypeAdapters in concurrent calls #1830

Closed
wants to merge 1 commit into from

Conversation

mwalkerr
Copy link

@mwalkerr mwalkerr commented Dec 1, 2020

Resolves #1829
Resolves #625 partially

In the current implementation, a TypeAdapter may be added to the typeTokenCache despite having a FutureTypeAdapter with a null delegate as part of its definition. This would occasionally cause us to see an IllegalStateException from GSON. This would happen when bringing up an application in a docker container and then running parallelized API tests against it. Some of the objects the application deserializes are recursively defined such as A -> B -> A. I believe what was happening was that one thread would start to deserialize A and call to getAdapter(A) which would call to getAdapter(B) which would call getAdapter(A), which would return the FutureTypeAdapter for A. Before the first thread is able to update the delegate for the FutureTypeAdapter for A, another thread tries to deserialize B, calls getAdapter(B), and retrieves the entry added by the first thread which contains a TypeAdapter for A which is a FutureTypeAdapter with a null delegate

@google-cla
Copy link

google-cla bot commented Dec 1, 2020

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added the cla: no label Dec 1, 2020
@google-cla
Copy link

google-cla bot commented Dec 1, 2020

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment @googlebot I fixed it.. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

2 similar comments
@google-cla
Copy link

google-cla bot commented Dec 1, 2020

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment @googlebot I fixed it.. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

@google-cla
Copy link

google-cla bot commented Dec 1, 2020

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment @googlebot I fixed it.. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

@mwalkerr
Copy link
Author

mwalkerr commented Dec 1, 2020

I used the following test to verify the issue and the solution:

package com.google.gson.regression;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import junit.framework.TestCase;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.CyclicBarrier;

public class RecursiveTypeRaceConditionTest extends TestCase {
  private final Gson gson = new Gson();

  public void testNullSafeBugSerialize() throws Exception {
    for (int i=0;i<100000;i++){
      Field f = gson.getClass().getDeclaredField("typeTokenCache");
      f.setAccessible(true);
      ((Map) f.get(gson)).clear();
      final CyclicBarrier gate = new CyclicBarrier(3);
      final boolean[] exception = {false};
      Thread t1 = new Thread(new Runnable() {
        @Override
        public void run() {
          try {
            gate.await();
            gson.getAdapter(TypeToken.get(A.class)).fromJson("{\"b\": {\"a\":{}}}");
          } catch (Exception e) {
            exception[0] = true;
            e.printStackTrace();
          }
        }
      });
      Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
          try {
            gate.await();
            gson.getAdapter(TypeToken.get(B.class)).fromJson("{\"a\": {\"b\":{}}}");
          } catch (Exception e) {
            exception[0] = true;
            e.printStackTrace();
          }
        }
      });
      t1.start();
      t2.start();
      gate.await();
      t1.join();
      t2.join();
      assertFalse(exception[0]);
    }
  }

  private static final class A {
    B b;
  }
  private static final class B {
    A a;
  }
}

@Marcono1234
Copy link
Collaborator

Thanks a lot for you work on this! This has now be fixed by #1832, but thank you nonetheless!

@Marcono1234 Marcono1234 closed this Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants