Skip to content

[GeoLocation] invalidate timer after success or error callback - #1226

Closed
henter wants to merge 1 commit into
react:masterfrom
henter:master
Closed

[GeoLocation] invalidate timer after success or error callback#1226
henter wants to merge 1 commit into
react:masterfrom
henter:master

Conversation

@henter

Copy link
Copy Markdown
Contributor

Just trying to getCurrentPosition , and found the errorBlock of location request in timeout handler would cause red error like this:

2015-05-10 17:50:39.607 [warn][tid:com.facebook.React.JavaScript] "Warning: Cannot find callback with CBID 5. Native module may have invoked both the success callback and the error callback."
2015-05-10 17:50:39.610 [error][tid:com.facebook.React.JavaScript] "Error: null is not an object (evaluating 'cb.apply')
stack: _invokeCallback index.ios.bundle:7593
<unknown> index.ios.bundle:7656
<unknown> index.ios.bundle:7648
perform index.ios.bundle:6157
batchedUpdates index.ios.bundle:13786
batchedUpdates index.ios.bundle:4689
<unknown> index.ios.bundle:7647
applyWithGuard index.ios.bundle:882
guardReturn index.ios.bundle:7421
processBatch index.ios.bundle:7646
URL: http://192.168.100.182:8081/index.ios.bundle
line: 7593
message: null is not an object (evaluating 'cb.apply')"

img_0837

I'm not sure if there is a better way to avoid multiple callback which caused the error,
but I don't think that the errorBlock should be called in timeout handler.
So, just remove it.

update: to invalidate the timer after success or error callback.

I just use the demo code of Geolocation

@facebook-github-botfacebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 10, 2015
@ide

ide commented May 11, 2015

Copy link
Copy Markdown
Contributor

This leaks memory on the JS side if the request times out and will also cause the JS app to hang forever if the callback isn't invoked. Better to cancel the timer if the location request invokes either its success or error callback.

@henter

Copy link
Copy Markdown
ContributorAuthor

@ide yeah, that's a great idea.
code updated. :-)

@henterhenter changed the title [GeoLocation] remove errorBlock in timeout handler[GeoLocation] invalidate timer after success or error callbackMay 11, 2015
@ide

ide commented May 15, 2015

Copy link
Copy Markdown
Contributor

@henter this looks good but can you please verify that the NSTimer is scheduled and invalidated on the same run loop (check with https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFRunLoopRef/index.html#//apple_ref/c/func/CFRunLoopGetCurrent)?

Apple says:

you should always call the invalidate method from the same thread on which the timer was installed

In -[RCTLocationRequest dealloc] you should check _timeoutTimer.valid since I believe it will crash the app if a timer is invalidated twice.

Also please squash your commits (git rebase -i) into one commit so that it's easier to review and bisect.

Otherwise, this PR looks good to me.

@henter

Copy link
Copy Markdown
ContributorAuthor

@ide thanks. code updated. :-)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ide

ide commented May 15, 2015

Copy link
Copy Markdown
Contributor

Left one small comment if you want to update it but will mark this as accepted.

@henter

Copy link
Copy Markdown
ContributorAuthor

updated.

@ide

ide commented May 15, 2015

Copy link
Copy Markdown
Contributor

@vjeux if I say:
@facebook-github-bot import
will it work? ;)

@vjeux

Copy link
Copy Markdown
Contributor

You need it in its own line. It actually may work with you since you have a fb employee vc

@ide

ide commented May 15, 2015

Copy link
Copy Markdown
Contributor

@facebook-github-bot import

@reconbot

Copy link
Copy Markdown

Looking forward to see this land. 👍

reconbot added a commit to wizarddevelopment/farmers-market-app that referenced this pull request May 18, 2015

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space after if and space after (

@henter

Copy link
Copy Markdown
ContributorAuthor

code updated. @ide@vjeux

@ide

ide commented May 20, 2015

Copy link
Copy Markdown
Contributor

Thanks @henter. This looks fine to me.

@vjeux

Copy link
Copy Markdown
Contributor

@facebook-github-bot import

@facebook-github-bot

Copy link
Copy Markdown
Contributor

Thanks for importing. If you are an FB employee go to https://our.intern.facebook.com/intern/opensource/github/pull_request/1402019833455864/int_phab to review.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new invalidate calls shouldn't be necessary because this should be invalidating the timers because all the requests should be dealloc'd at this point. If that's not happening, there is probably a retain cycle or something? cc @nicklockwood

@sahrens

Copy link
Copy Markdown
Contributor

Did you have a repro case that failed 100% without this PR and 0% with it? I'm afraid there might be something else going wrong.

@vjeuxvjeux assigned sahrens and unassigned ideMay 20, 2015
@sahrens

Copy link
Copy Markdown
Contributor

ping @ide, @henter on my comment - am I missing something?

@ide

ide commented May 22, 2015

Copy link
Copy Markdown
Contributor

@sahrens My view on this is: covering up the retain cycle (if there is one) is bad and we should fix that. I do think the explicit -[invalidate] calls clarify the intent here, since we want to stop the timer whether or not the request is deallocated.

I haven't profiled the code for leaks but it looks like there is a cycle between request.timer and timer.userInfo (which is the request object). A nice thing about calling -[NSTimer invalidate] is that it clears the strong references to the timer's target and userInfo properties, which should break the cycle.

@sahrens

Copy link
Copy Markdown
Contributor

Cool, thanks - I'll bring this in.

On May 22, 2015, at 4:52 PM, James Ide notifications@github.com wrote:

@sahrens My view on this is: covering up the retain cycle (if there is one) is bad and we should fix that. I do think the explicit -[invalidate] calls clarify the intent here, since we want to stop the timer whether or not the request is deallocated.

I haven't profiled the code for leaks but it looks like there is a cycle between request.timer and timer.userInfo (which is the request object). A nice thing about calling -[NSTimer invalidate] is that it clears the strong references to the timer's target and userInfo properties, which should break the cycle.


Reply to this email directly or view it on GitHub.

tadeuzagallo pushed a commit to tadeuzagallo/react-native that referenced this pull request May 28, 2015
Summary:
Just trying to [getCurrentPosition](https://github.com/facebook/react-native/blob/master/Libraries/Geolocation/Geolocation.js#L45) , and found the `errorBlock` of location request in timeout handler would cause red error like this:
```
2015-05-10 17:50:39.607 [warn][tid:com.facebook.React.JavaScript] "Warning: Cannot find callback with CBID 5. Native module may have invoked both the success callback and the error callback."
2015-05-10 17:50:39.610 [error][tid:com.facebook.React.JavaScript] "Error: null is not an object (evaluating 'cb.apply')
stack:
_invokeCallback index.ios.bundle:7593
<unknown> index.ios.bundle:7656
<unknown> index.ios.bundle:7648
perform index.ios.bundle:6157
batchedUpdates index.ios.bundle:13786
batchedUpdates index.ios.bundle:4689
<unknown> index.ios.bundle:7647
applyWithGuard index.ios.bundle:882
guardReturn index.ios.bundle:7421
processBatch index.ios.bundle:7646
URL: http://192.168.100.182:8081/indexClosesreact#1226
Github Author: henter <henter@henter.me>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
@justinhj

Copy link
Copy Markdown

I've updated to the latest publicly available React native, but this crash still happens using the demo code provided.

@aphillipo

Copy link
Copy Markdown

Still seems to be broken for me too. Did this make it into the release, should I use beta/github react-native from npm?

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

Labels

CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

@henter@ide@vjeux@reconbot@facebook-github-bot@sahrens@justinhj@aphillipo@reactjs-bot