kiteConnect.generateSession(requestToken, api_secret) spawns threads that take 5 minutes to expire.

nkr
Hi,

When I use the line kiteConnect.generateSession(requestToken, api_secret) in my code, it spaws OKHttp taskrunner and UrlChecker threads that die out after exactly 5 minutes after the execution of the above mentioned line of code. When the program runs long (places orders and receives web ticker), these threads dont die at all which raises the issue of the clean exit of my program. Can you please guide a little on how these threads are supposed to work, how is their ttyl decided and how can I manage them to do a clean exit of my code?
  • rakeshr
    @nkr
    Which version of javakiteconnect are you using?
    This unclean exit shouldn't be there on our latest version.
  • nkr
    nkr edited November 13
    Am using version 3.1.3 of java kite connect. The threads that don't exit are :

    OkHttp TaskRunner
    OkHttp api.kite.trade
    OkHttp TaskRunner
    Okio Watchdog
    OkHttp TaskRunner

    Code :
    	public static void testbar()
    {
    KiteConnect kiteSdk = new KiteConnect(api_key);
    kiteSdk.setUserId(kite_username);

    String url = kiteSdk.getLoginURL();

    User userModel;
    try {
    userModel = kiteSdk.generateSession(requestToken, api_secret);

    } catch (JSONException | IOException | KiteException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    Thread.getAllStackTraces().keySet().forEach((t) -> System.out.println(t.getName()));

    // Set session expiry callback.
    kiteSdk.setSessionExpiryHook((SessionExpiryHook) new SessionExpiryHook() {
    @Override
    public void sessionExpired() {
    System.out.println("session expired");
    }
    });
    }
  • sujith
    Can you let us know more about your os, Java and JDK details?
  • nkr
    nkr edited November 13
    Here is the output of uname -a :
    Linux PC 5.9.2-arch1-1 #1 SMP PREEMPT Thu, 29 Oct 2020 17:01:28 +0000 x86_64 GNU/Linux
    Here is the output of java --version :
    openjdk 14.0.2 2020-07-14
    OpenJDK Runtime Environment (build 14.0.2+12)
    OpenJDK 64-Bit Server VM (build 14.0.2+12, mixed mode)


    I created a new project with the following maven dependency to make sure I wasnt using the old libraries :

    <code class="CodeInline">
    <!-- https://mvnrepository.com/artifact/com.zerodhatech.kiteconnect/kiteconnect -->
    <dependency>
    <groupId>com.zerodhatech.kiteconnect</groupId>
    <artifactId>kiteconnect</artifactId>
    <version>3.1.3</version>
    </dependency>





    Does the testbar() which I wrote above reproduce the issue at your end?
  • sujith
    But then how did you conclude that it takes 5 minutes to expire?
    Signal Dispatcher
    OkHttp TaskRunner
    main
    Finalizer
    Reference Handler
    Okio Watchdog
    Monitor Ctrl-Break

    Process finished with exit code 0
    This is the output for me.
  • nkr
    nkr edited November 13
    If you look at the runtime in the attached image in my previous reply, its from 10:35:49 to 10:40:50. If I use proper logging, this time is always exactly 5 minutes.
  • sujith
    KiteRequestHandler class has the OkHttpClient which is used to make requests and that is created only once and reused.
    Can you try to fork javakiteconnect and try calling client.getDispatcher().getExecutorService().shutdown(); once you are done?

    source: https://github.com/square/okhttp/issues/1739
  • sujith
    Let us know if it works.
Sign In or Register to comment.