offbynull / coroutines

Java toolkit that allows you to write coroutines.
GNU Lesser General Public License v3.0
346 stars 53 forks source link

Strange ClassCastException #76

Closed thelastender closed 7 years ago

thelastender commented 7 years ago

Hello, I have encountered a strange java.lang.ClassCastException in my code. The code is show below:

public class MyCoroutine implements Coroutine
{
               @Override
        public void run(Continuation c)
        {
                        ObjA objA = new ObjA();
                        if(testCaseOne())
                        {
                                  Map<Integer, Integer> map = new HashMap<>();
                                  //.... do something with the map.
                                  objA.map = map;
                         }
                         else
                         {
                                   if(testCaseTwo())
                                   {
                                             ObjB objB = getObjBFromSomeOther();
                                             if(objB == null)
                                             { 
                                                        throw new RuntimeException("e2");
                                             }
                                             //...... do something with the objB
                                             //...... do something other
                                   }
                                   else
                                   {
                                             throw new RuntimeException("e1");
                                   }
                         }

                         pause(c, objA);
                }

                 public void pause(Continuation c, ObjA objA)
                 {
                          //...... do something
                          c.suspend();
                          // exception at here: java.lang.ClassCastException can't cast ObjB to java.util.Map 
                          //...... do something
                 }
}

After c.suspend(), I got a strange ClassCastException: ObjB can not be cast to java.util.Map. I changed the Map's position to here:

// ......
                @Override
        public void run(Continuation c)
        {
                        ObjA objA = new ObjA();
                        Map<Integer, Integer> map;
                        if(testCaseOne())
                        {
                                  map = new HashMap<>();
                                  //.... do something with the map.
                                  objA.map = map;
                         }
// ......

It is ok, no Exception caught.

So I want to known how this happens?

thelastender commented 7 years ago

I use 1.1.1 and my code is running in single thread.

offbynull commented 7 years ago

Hi,

You've probably hit a bug with the instrumentation logic. I'll take a look at this when I get home tonight. In the mean time, could you please...

  1. tell me which version of the jdk you're compiling with
  2. try using the newest version (1.2.2) released yesterday

Thanks!

On Jan 9, 2017 9:54 AM, "Ender" notifications@github.com wrote:

I use 1.1.1 and my code is running in single thread.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/offbynull/coroutines/issues/76#issuecomment-271355104, or mute the thread https://github.com/notifications/unsubscribe-auth/AGD4IuQc9npGppOihdEaG0RIipeIu8dqks5rQnRJgaJpZM4LeiHs .

thelastender commented 7 years ago

My jdk is mac version 1.8.0_25 64bit from Oracle. I use 1.2.2 and tested. The result is the same. My asm version is 5.0.3.

Thank you for help!

offbynull commented 7 years ago

Okay. Thanks.

I'll have more information once I get a chance to look in to this.

On Jan 9, 2017 10:23 AM, "Ender" notifications@github.com wrote:

My jdk is mac version 1.8.0_25 64bit from Oracle. I use 1.2.2 and tested. The result is the same. My asm version is 5.0.3.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/offbynull/coroutines/issues/76#issuecomment-271362719, or mute the thread https://github.com/notifications/unsubscribe-auth/AGD4IjObu1iRuZPSMd2N5ZyFnAIa2GjUks5rQnsYgaJpZM4LeiHs .

offbynull commented 7 years ago

Could you please post the full source?

On Jan 9, 2017 10:24 AM, "Kasra Faghihi" offbynull@gmail.com wrote:

Okay. Thanks.

I'll have more information once I get a chance to look in to this.

On Jan 9, 2017 10:23 AM, "Ender" notifications@github.com wrote:

My jdk is mac version 1.8.0_25 64bit from Oracle. I use 1.2.2 and tested. The result is the same. My asm version is 5.0.3.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/offbynull/coroutines/issues/76#issuecomment-271362719, or mute the thread https://github.com/notifications/unsubscribe-auth/AGD4IjObu1iRuZPSMd2N5ZyFnAIa2GjUks5rQnsYgaJpZM4LeiHs .

thelastender commented 7 years ago

I can send you a email of the full source. I can't post here because of some sensitive things. So, what's your email?

offbynull commented 7 years ago

Hi,

I took the code sample you posted and tried to run it. I didn't get a ClassCastException. I tried both the testCaseOne() path and the testCaseTwo() path (code below).

My email is offbynull@gmail.com. Please send a fully compile-able example (build script included). If you need me to sign a confidentiality agreement / non-disclosure agreement, please send that before hand and I'll sign it and get it back to you.

Thanks.

package com.offbynull.coroutinestest;

import com.offbynull.coroutines.user.Continuation;
import com.offbynull.coroutines.user.Coroutine;
import java.util.HashMap;
import java.util.Map;

public class MyCoroutine implements Coroutine {

    @Override
    public void run(Continuation c) {
        ObjA objA = new ObjA();
        if (testCaseOne()) {
            Map<Integer, Integer> map = new HashMap<>();
            //.... do something with the map.
            objA.map = map;
        } else {
            if (testCaseTwo()) {
                ObjB objB = getObjBFromSomeOther();
                if (objB == null) {
                    throw new RuntimeException("e2");
                }
                //...... do something with the objB
                //...... do something other
            } else {
                throw new RuntimeException("e1");
            }
        }

        pause(c, objA);
    }

    public void pause(Continuation c, ObjA objA) {
        //...... do something
        c.suspend();
        // exception at here: java.lang.ClassCastException can't cast ObjB to java.util.Map 
        //...... do something
    }

    private boolean testCaseOne() {
        return false;
    }

    private boolean testCaseTwo() {
        return true;
    }

    private ObjB getObjBFromSomeOther() {
        return new ObjB();
    }

    public static final class ObjA {
        public Map<Integer, Integer> map;
    }

    public static final class ObjB {

    }
}
offbynull commented 7 years ago

Also tested when getObjBFromSomeOther() returns null. It throws the correct RuntimeException.

offbynull commented 7 years ago

I'm not sure if you've sent anything, but I haven't gotten an email yet.

offbynull commented 7 years ago

No response -- closing this ticket.