Patch for race condition #5

Closed
vincentjames501 opened this Issue Feb 27, 2014 · 1 comment

Projects

None yet

2 participants

@vincentjames501

I'm not an expert in this at all which is why I'm not submitting this as a pull request but rather as a review. I keep getting exceptions on the line below because stopMonitoringNetworkReachability was called and then s_self->_handler_blk is NULL inside the dispatch_async. Clearly this is a race condition.

if (s_self) dispatch_async(dispatch_get_main_queue(), ^{s_self->_handler_blk(status);});

From within this code block:

void(^cb_blk)(GCNetworkReachabilityStatus) = ^(GCNetworkReachabilityStatus status) {

        GCNetworkReachability *s_self = w_self;
        if (s_self) dispatch_async(dispatch_get_main_queue(), ^{s_self->_handler_blk(status);});
    };

I believe we could change it to something like this:

    void(^cb_blk)(GCNetworkReachabilityStatus) = ^(GCNetworkReachabilityStatus status) {

        GCNetworkReachability *s_self = w_self;
        if (s_self) {
            void(^_blk_cpy)(GCNetworkReachabilityStatus) = s_self->_handler_blk;
            if(_blk_cpy) {
                dispatch_async(dispatch_get_main_queue(), ^{_blk_cpy(status);});
            }
        }
    };

so that we hold onto a reference in the dispatch_async and make sure the _blk_cpy is not NULL

Thoughts?

@GlennChiu
Owner

Fixed

@GlennChiu GlennChiu closed this Jun 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment