Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LTOBD2Adapter: Race Condition in Command Queue Access #22

Open
Skyb0rg opened this issue Dec 20, 2018 · 7 comments
Open

LTOBD2Adapter: Race Condition in Command Queue Access #22

Skyb0rg opened this issue Dec 20, 2018 · 7 comments
Labels

Comments

@Skyb0rg
Copy link

Skyb0rg commented Dec 20, 2018

-(void)asyncEnqueueInternalCommand:(LTOBD2AdapterInternalCommand*)internalCommand
{
    **[_commandQueue addObject:internalCommand];**
    [self asyncProcessCommandQueue];
}
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM insertObject:atIndex:]: index 7191 beyond bounds [0 .. 7189]'

Can you help me to figure out what has happend. I record over 30 Minutes rpm and speed and 4 Other values.

@Skyb0rg
Copy link
Author

Skyb0rg commented Dec 20, 2018

Tested this in debug and _commandQueue is between 1 and 6 after adding commands.
i dont know why it can go up to 7000.
And why it is possible to get this Error: index beyond bounds

i will try it with this solution:

if ( [_commandQueue count] > 200 )
{
    NSLog( @"cancelPendingCommands : Has more than 200" );
    [self cancelPendingCommands];
}

after: asyncProcessCommandQueue

@mickeyl
Copy link
Owner

mickeyl commented Dec 21, 2018

Thanks for this report. The actual problem looks like a multithreading race condition – I should check whether access of the command queue is a) coordinated or b) make the command queue thread-safe.

That said, if you're checking values in a loop, you have to take care that you're not adding commands faster than they are handled, so instead of adding based on a timer it would be better to use the completion handler to schedule the next iteration.

Last but not least, there are several optimizations that I want to tackle in 2019, namely adding facilities to group multiple PID queries (in order to save bandwidth), and also something to ease repeating queries.

@mickeyl mickeyl changed the title Error: sometimes i got a crash in LTOBD2Adapter.m NSRangeException beyond bounds LTOBD2Adapter: Race Condition in Command Queue Access Dec 21, 2018
@mickeyl mickeyl added the bug label Dec 21, 2018
@Skyb0rg
Copy link
Author

Skyb0rg commented Dec 22, 2018

created an outlet for the queue count and slow down the new requests if needed.
for me its currently ok.
Thank you for detailed answer 👍

@Skyb0rg
Copy link
Author

Skyb0rg commented Jan 3, 2019

Have you any idea how to fix this ?
Yesterday it was 0...4 [index 6 beyond bounds]

I start the "transmitMultipleCommands" function in the main queue but it doesn´t help.

@Skyb0rg
Copy link
Author

Skyb0rg commented Feb 1, 2019

This Error only happens all >50.000 readings so its hard to find the error but i think this might be help.

-(void)asyncEnqueueInternalCommand:(LTOBD2AdapterInternalCommand*)internalCommand
{
    @synchronized(self) {
        [_commandQueue addObject:internalCommand];
        [self asyncProcessCommandQueue];
    }
}

@Skyb0rg
Copy link
Author

Skyb0rg commented Feb 15, 2019

@synchronized(self) is not the solution.
Now i test with the serial dispatchqueue.

@Skyb0rg
Copy link
Author

Skyb0rg commented Mar 15, 2019

created pull request for the solution. #26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants