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

[FIX] Add a timeout when trying to retrieve dcm2bids version #154

Merged
merged 9 commits into from
May 30, 2022

Conversation

po09i
Copy link
Contributor

@po09i po09i commented Apr 11, 2022

Description

This PR adresses #149.

There is a bug that causes dcm2bids to hang when your computer is connected via ethernet to another device, but that device does not provide internet. The bug occurs when fetching the version of dcm2bids or dcm2niix. To fix this, a timeout is added to prevent hanging. While investigating, I noticed the internet() function always returns True and is not necessary, so it was removed. In more details this PR:

  • Adds a timeout option when calling check_github_latest() to prevent hanging indefinitely
  • Removes the internet() function as it does nothing and is not useful
    • Removes all (1) tests of the internet() function

Note:
We realized the problem does not occur for everyone, it happens when using a vpn, or at least on Cisco anyconnect.. See this #149 (comment) for more detail.

To replicate the problem scenario

  1. Connect your computer to your router using an ethernet cable
  2. Disconnect from the router the cable providing internet to your router
  3. Run dcm2bids

Linked issues

Fixes #149

@arnaudbore
Copy link
Contributor

Hi @po09i,

Thank you for your PR. I'm not able to replicate the issue on my laptop.
Everything seems to work as expected, when no access to internet internet() returns False within 3s. I will let your PR a little bit longer to see if more people have the same issue.

Copy link
Member

@SamGuay SamGuay left a comment

Choose a reason for hiding this comment

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

I tried the PR on my machine and when I'm offline, I get an error:
Thus, I'm suggesting we ping first, then curl.

For some reason, I couldn't suggest changes throughout the files, so it needs a bit of tidying.

INFO:dcm2bids.dcm2bids:moving acquisitions into BIDS folder
DEBUG:dcm2bids.version:Checking latest version of unfmontreal/Dcm2Bids was not possible
Traceback (most recent call last):
  File "/home/sam/Projects/Dcm2Bids/dcm2bids/version.py", line 43, in check_github_latest
    output = check_output(shlex.split("curl --silent " + url), timeout=timeout)
  File "/home/sam/miniconda3/envs/dcm2bids-dev/lib/python3.10/subprocess.py", line 420, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/home/sam/miniconda3/envs/dcm2bids-dev/lib/python3.10/subprocess.py", line 524, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['curl', '--silent', 'https://github.com/unfmontreal/Dcm2Bids/releases/latest']' returned non-zero exit status 6.
DEBUG:dcm2bids.version:Checking latest version of rordenlab/dcm2niix was not possible
Traceback (most recent call last):
  File "/home/sam/Projects/Dcm2Bids/dcm2bids/version.py", line 43, in check_github_latest
    output = check_output(shlex.split("curl --silent " + url), timeout=timeout)
  File "/home/sam/miniconda3/envs/dcm2bids-dev/lib/python3.10/subprocess.py", line 420, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/home/sam/miniconda3/envs/dcm2bids-dev/lib/python3.10/subprocess.py", line 524, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['curl', '--silent', 'https://github.com/rordenlab/dcm2niix/releases/latest']' returned non-zero exit status 6.

Comment on lines 43 to 46
output = check_output(shlex.split("curl --silent " + url), timeout=timeout)
except:
logger.debug(
"Checking latest version of %s was not possible", githubRepo,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
output = check_output(shlex.split("curl --silent " + url), timeout=timeout)
except:
logger.debug(
"Checking latest version of %s was not possible", githubRepo,
try:
if os.name == "nt":
check_call(shlex.split("ping -n 1 8.8.8.8"),
stdout=DEVNULL,
stderr=STDOUT,
timeout=timeout)
else:
check_call(shlex.split("ping -c 1 8.8.8.8"),
stdout=DEVNULL,
stderr=STDOUT,
timeout=timeout)
except:
logger.info(f"Checking latest version of {githubRepo} was not possible")
logger.debug("Can't connect to internet", exc_info=True)
return
try:
output = check_output(shlex.split("curl --silent " + url), timeout=timeout)
return (

Copy link
Member

Choose a reason for hiding this comment

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

need to import
from subprocess import check_output, PIPE, check_call, STDOUT, DEVNULL as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The error is the result of the following line while using the -l DEBUG option. It displayed the error but the code handled it.

logger.debug("Checking latest version of %s was not possible", githubRepo, exc_info=True)

When we remove the -l DEBUG, we don't see the error message.

I believe 'pinging' adds an extra layer and extra timeout time for no added benefit. I have opted not to add it until further feedback. However, I have split it up like you suggest to have feedback in 'normal' use cases and output the error message in debug mode:

logger.info(f"Checking latest version of {githubRepo} was not possible")
logger.debug("Can't connect to internet", exc_info=True)

@po09i
Copy link
Contributor Author

po09i commented Apr 25, 2022

Here are the different outputs for different use cases (I'm only including the last lines of the output log to not clog up this already big comment):

  • internet

--No extra output--

  • No internet (info)
INFO:dcm2bids.version:Checking latest version of unfmontreal/Dcm2Bids was not possible
INFO:dcm2bids.version:Checking latest version of rordenlab/dcm2niix was not possible
  • No internet (debug)
INFO:dcm2bids.version:Checking latest version of unfmontreal/Dcm2Bids was not possible
DEBUG:dcm2bids.version:Error while 'curl --silent https://github.com/unfmontreal/Dcm2Bids/releases/latest'
Traceback (most recent call last):
  File "/Users/alexandredastous/Documents/School/Polytechnique/Master/project/Dcm2Bids/dcm2bids/version.py", line 43, in check_github_latest
    output = check_output(shlex.split("curl --silent " + url), timeout=timeout)
  File "/Users/alexandredastous/miniconda3/envs/dcm2bids/lib/python3.9/subprocess.py", line 424, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/Users/alexandredastous/miniconda3/envs/dcm2bids/lib/python3.9/subprocess.py", line 528, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['curl', '--silent', 'https://github.com/unfmontreal/Dcm2Bids/releases/latest']' returned non-zero exit status 6.
INFO:dcm2bids.version:Checking latest version of rordenlab/dcm2niix was not possible
DEBUG:dcm2bids.version:Error while 'curl --silent https://github.com/rordenlab/dcm2niix/releases/latest'
Traceback (most recent call last):
  File "/Users/alexandredastous/Documents/School/Polytechnique/Master/project/Dcm2Bids/dcm2bids/version.py", line 43, in check_github_latest
    output = check_output(shlex.split("curl --silent " + url), timeout=timeout)
  File "/Users/alexandredastous/miniconda3/envs/dcm2bids/lib/python3.9/subprocess.py", line 424, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/Users/alexandredastous/miniconda3/envs/dcm2bids/lib/python3.9/subprocess.py", line 528, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['curl', '--silent', 'https://github.com/rordenlab/dcm2niix/releases/latest']' returned non-zero exit status 6.
  • No internet but on local network (info) -- It took 6 more seconds due to the timeouts
INFO:dcm2bids.version:Checking latest version of unfmontreal/Dcm2Bids was not possible
INFO:dcm2bids.version:Checking latest version of rordenlab/dcm2niix was not possible
  • No internet but on local network (debug) -- [Outdated!]
DEBUG:dcm2bids.version:Error while 'curl --silent https://github.com/unfmontreal/Dcm2Bids/releases/latest'
Traceback (most recent call last):
  File "/Users/alexandredastous/Documents/School/Polytechnique/Master/project/Dcm2Bids/dcm2bids/version.py", line 43, in check_github_latest
    output = check_output(shlex.split("curl --silent " + url), timeout=timeout)
  File "/Users/alexandredastous/miniconda3/envs/dcm2bids/lib/python3.9/subprocess.py", line 424, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/Users/alexandredastous/miniconda3/envs/dcm2bids/lib/python3.9/subprocess.py", line 507, in run
    stdout, stderr = process.communicate(input, timeout=timeout)
  File "/Users/alexandredastous/miniconda3/envs/dcm2bids/lib/python3.9/subprocess.py", line 1134, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "/Users/alexandredastous/miniconda3/envs/dcm2bids/lib/python3.9/subprocess.py", line 1980, in _communicate
    self._check_timeout(endtime, orig_timeout, stdout, stderr)
  File "/Users/alexandredastous/miniconda3/envs/dcm2bids/lib/python3.9/subprocess.py", line 1178, in _check_timeout
    raise TimeoutExpired(
subprocess.TimeoutExpired: Command '['curl', '--silent', 'https://github.com/unfmontreal/Dcm2Bids/releases/latest']' timed out after 3 seconds
INFO:dcm2bids.version:Checking latest version of rordenlab/dcm2niix was not possible
DEBUG:dcm2bids.version:Error while 'curl --silent https://github.com/rordenlab/dcm2niix/releases/latest'
Traceback (most recent call last):
  File "/Users/alexandredastous/Documents/School/Polytechnique/Master/project/Dcm2Bids/dcm2bids/version.py", line 43, in check_github_latest
    output = check_output(shlex.split("curl --silent " + url), timeout=timeout)
  File "/Users/alexandredastous/miniconda3/envs/dcm2bids/lib/python3.9/subprocess.py", line 424, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/Users/alexandredastous/miniconda3/envs/dcm2bids/lib/python3.9/subprocess.py", line 507, in run
    stdout, stderr = process.communicate(input, timeout=timeout)
  File "/Users/alexandredastous/miniconda3/envs/dcm2bids/lib/python3.9/subprocess.py", line 1134, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "/Users/alexandredastous/miniconda3/envs/dcm2bids/lib/python3.9/subprocess.py", line 1980, in _communicate
    self._check_timeout(endtime, orig_timeout, stdout, stderr)
  File "/Users/alexandredastous/miniconda3/envs/dcm2bids/lib/python3.9/subprocess.py", line 1178, in _check_timeout
    raise TimeoutExpired(
subprocess.TimeoutExpired: Command '['curl', '--silent', 'https://github.com/rordenlab/dcm2niix/releases/latest']' timed out after 3 seconds

Note: The error messages are there for debug purposes, they are not an actual error.

@SamGuay
Copy link
Member

SamGuay commented Apr 28, 2022

I agree that adding the ping adds another layer.. However not all scenarios raise subprocess.TimeoutExpired: however in one situation, it raises subprocess.CalledProcessError which is the scenario I encountered when testing that led me to add the ping. It works as supposed given there is a bare except at the end but I think it should be for TimeoutExpired..

I don't have strong feeling about it though so I will let @arnaudbore decide 😁

@po09i
Copy link
Contributor Author

po09i commented Apr 28, 2022

@SamGuay

It works as supposed given there is a bare except at the end but I think it should be for TimeoutExpired..

Good point about the empty except clause, I can add both exceptions in the except clause so that we follow proper guidelines.

@po09i
Copy link
Contributor Author

po09i commented Apr 28, 2022

Since I could separated the exceptions, I wrote a different error message for the timeout. Since the timeout is self explanatory, I removed the output of the error message. Here is the updated output following the comments:

  • No internet but on local network (debug)

Screen Shot 2022-04-28 at 5 34 49 PM

  • Refer to this comment for all other outputs

@codecov-commenter
Copy link

codecov-commenter commented May 26, 2022

Codecov Report

❗ No coverage uploaded for pull request base (master@abd6994). Click here to learn what that means.
The diff coverage is n/a.

@@            Coverage Diff            @@
##             master     #154   +/-   ##
=========================================
  Coverage          ?   68.00%           
=========================================
  Files             ?       10           
  Lines             ?      675           
  Branches          ?      108           
=========================================
  Hits              ?      459           
  Misses            ?      185           
  Partials          ?       31           
Flag Coverage Δ
pytest 68.00% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update abd6994...e4ad28b. Read the comment docs.

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

Successfully merging this pull request may close these issues.

dcm2bids hangs when connected via ethernet without internet
4 participants