Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Fix uv_async crash (aka Option B) #2400

Closed

Description

@bleege

While investigating #2295 @ljbade discovered that OkHttp and uv_async were not working properly together. The result was the decision to ship 0.1.0 using curl instead. This ticket will be about restoring OkHttp by working out the issues with uv_async as documented by @ljbade. Specifically:

  • on curl all completed HTTPRequest handling happens in the same thread that creates HTTPContext as curl uses a loop pump to process everything (this is enforced via MBGL_VERIFY_THREAD macro)
  • on NSUrl and OkHTTP we farm HTTPRequest out to the system so we get the result back async on a different thread from the one that created HTTPContext
  • that is why both NSUrl and OkHTTP use uv_async to hand the final request processing back to the HTTPContext thread

So to remove uv_async we need an alternative way to get the request back to the HTTPContext thread.

I think @kkaefer will need to help us here with Option C, or he might be able to persue Option B.

@incanus @ljbade @kkaefer

Activity

  1. added
    Android
    on Sep 23, 2015
  2. added this to the milestone on Sep 23, 2015
  3. jfirebaugh commented on Sep 23, 2015

    @jfirebaugh
    Contributor

    uv_async is a fine way to communicate results back to the originating thread, and "remove use of uv_async" is not the correct long-term fix for "undiagnosed crash connected to uv_async use". Instead, we should diagnose the root cause of the crash.

  4. ljbade commented on Sep 23, 2015

    @ljbade
    Contributor

    @jfirebaugh Yes let's fix the uv_async bug.

  5. changed the title [-]Refactor OkHttp To Not Use uv_async (aka Option C)[/-] [+]Fix uv_async crash (aka Option B)[/+] on Sep 23, 2015
  6. ljbade commented on Sep 23, 2015

    @ljbade
    Contributor

    @kkaefer Where else have you noticed uv_async crashing before? iOS/OSX/Linux?

  7. ljbade commented on Oct 24, 2015

    @ljbade
    Contributor

    I should revisit this since @mikemorris updated the libuv version.

  8. 34 remaining items

  9. ljbade commented on Nov 10, 2015

    @ljbade
    Contributor

    @bleege I realise I should have not used read()

    The line in question is

    That line does the actual IO work of downloading the HTTP request. If something happens during that function either because of a network failure, or a cancellation request that function will throw an IOException

    You can see that the onReponse callback has a throws IOException thus we are expected to forward the exception after we handle it. It we don't rethrow the IOException, OkHTTP might not correctly clean up when a request fails.

    @zugaldia Do you agree with my assesment?

  10. zugaldia commented on Nov 11, 2015

    @zugaldia
    Member

    I've been running some scenarios and I've added a new application interceptor to OkHttp to help us with debugging: #2905 (comment)

    To clarify the situation, we are not seeing the "callback gets called twice" situation anymore, instead, when we find an exception in onReponse we catch it and call onFailure manually to handle it. This is because OkHttp wouldn't do it otherwise -- it'd only call one ("signal the callback" in their terminology).

    What I'm not sure at this point is if whether we should rethrow the IOException within onReponse. If I understand correctly OkHttp's behavior, that exception will simply get "swallowed" (see lysine-dev/okhttp#1335) meaning that it just gets logged without further cleaning (the relevant method is here: https://github.com/square/okhttp/blob/master/okhttp/src/main/java/com/squareup/okhttp/Call.java#L159). That's why @bleege saw #2400 (comment) which is in turn harmless.

    Another approach could be not to rethrow it and do a response.body().close() and nativeOnResponse() call only if no exception was found in onResponse. Otherwise, the SDK user will see exceptions logs potentially believing an exception was unhandled, when it was by our own onFailure.

  11. bleege commented on Nov 12, 2015

    @bleege
    ContributorAuthor

    @zugaldia @ljbade This is sounding like OkHttp is on pretty firm ground here. There's been no crashes reported on the internal testing app so far. If you two are both feeling good about this (I assume you both are?), let's polish the code up and get it into master.

    Great job @zugaldia @ljbade for making this happen! 🚀

  12. incanus commented on Nov 12, 2015

    @incanus
    Contributor
  13. added a commit that references this issue on Nov 12, 2015
    a950128
  14. ljbade commented on Nov 12, 2015

    @ljbade
    Contributor

    @zugaldia Thanks for digging into. Since it seems like everyone gets confused by OkHTTP logging the exception despite it being harmless, perhaps we should just swallow the exception? If you compare master to 2400-lock you will see I used to just return. However I suspect that might have been what caused the double callback somehow.

  15. tobrun commented on Nov 12, 2015

    @tobrun
    Member

    Great job @zugaldia @ljbade for making this happen! 🚀

    👍

    perhaps we should just swallow the exception?

    Seems logical

  16. zugaldia commented on Nov 12, 2015

    @zugaldia
    Member

    Agreed. As the exception is directed to onFailure I don't think there's any need for the extra logging. Also, it's consistent with the way OkHttp works and therefore it'd match the dev expectations.

  17. mb12 commented on Nov 12, 2015

    @mb12

    @ljbade , @tobrun , @bleege Is it possible to measure the performance overhead of OkHttp as well? In this case we will be copying 25K to 100K+ bytes for each tile from java to native using JNI versus the older one where this copy and the additional overhead of managed code(GC, etc.) is missing.

  18. ljbade commented on Nov 12, 2015

    @ljbade
    Contributor

    @zugaldia I just pushed a commit with return instead of rethrow... can you check it doesnt bring back double onresponse/onfailure?

  19. zugaldia commented on Nov 16, 2015

    @zugaldia
    Member

    @ljbade FWIW I just did a fresh build with the latest code and I couldn't find the double callback call.

  20. ljbade commented on Nov 16, 2015

    @ljbade
    Contributor

    @zugaldia Excellent I think we are ready to merge, except for figuring out how to benchmark the perf improvements. However we could do this after merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Androidcrash

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions