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

Solved sporadic crash when setmode is called multiple times #76

Merged
merged 1 commit into from
Sep 29, 2022

Conversation

ma-ludw
Copy link

@ma-ludw ma-ludw commented Aug 10, 2022

I had the problem, that sometimes if I wanted to set an output the software crashed with the exception "Channel 26 is invalid".
I tracked down the problem to the is_in() function in PythonFunctions.h. The moment the exception is thrown, the is_in() functions received an empty dictionary. I tested this by printing the size of the dictionary in the is_in() function.

I'm not sure why this happens. Because the _channel_data dictionary is only updated on startup and on every setmode(), I added a check to change the dictionary only once. This should not be a problem in my opinion, as you cannot update the mode once it has been set.

After the change, the problem no longer occurred. However, since it occurred only sporadically, I cannot say with 100% certainty that it is solved.

@pjueon
Copy link
Owner

pjueon commented Aug 12, 2022

Thanks for the report.

Ok, for confirmation

  • You got the exception when you called setup to set the channel 26 as an output.
  • The exception was thrown from:
    ChannelInfo MainModule::_channel_to_info_lookup(const string& channel, bool need_gpio, bool need_pwm)
    {
    if (!is_in(channel, _channel_data))
    throw runtime_error("Channel " + channel + " is invalid");
  • When the exception thrown, global()._channel_data was empty

Is my understanding correct?

And

  • Did you call setmode multiple times in your code?
  • Can you reproduce the issue if you revert your change?
  • Are you using setmode or setup asynchronously in your code?

@pjueon pjueon added the bug Something isn't working label Aug 12, 2022
@ma-ludw
Copy link
Author

ma-ludw commented Aug 15, 2022

Your understanding is correct. The exception is only thrown when the setmode is called multiple times. The exception was never thrown on the first call.

Yes, the bug is reproducible, but sometimes takes longer to occur.

Are you using setmode or setup asynchronously in your code?
I always call setmode before setup, but sometimes multiple times. So e.g., setmode, setup, setmode, setup.

Thanks for your help

@pjueon
Copy link
Owner

pjueon commented Aug 17, 2022

Could you give us any short and simple code example that can reproduce this bug?

I tried this code but I didn't get any exception:

#include <iostream>
#include <JetsonGPIO.h>


int main()
{
    constexpr int pin_num = 32;
    constexpr int N = 30;

    // no cleanup among iterations for check.
    for(int i = 0; i < N; i++)
    {
        std::cout << "i: " << i << std::endl;

        std::cout << "setmode start" << std::endl;    
        GPIO::setmode(GPIO::BOARD);
        std::cout << "setmode end" << std::endl;    

        std::cout << "setup start" << std::endl;    
        GPIO::setup(pin_num, GPIO::OUT);
        std::cout << "setup end" << std::endl;    
    }

    std::cout << "done" << std::endl;
    return 0;
}

Copy link
Owner

@pjueon pjueon left a comment

Choose a reason for hiding this comment

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

Failed to reproduced the issue, but the code change still looks good to me.
Because if you call setmode with the same mode multiple times, you would expect nothing to happen.

@pjueon pjueon merged commit 5af3a13 into pjueon:master Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants