How to debug PyGitHub not being responsive?

Viewed 289

I'm using PyGitHub to update my GitHub.com repos from inside a Ubuntu server using a python script.

I have noticed there are times, when my script just hangs there and there's no error message indicating what went wrong.

This is my script

from pathlib import Path
from typing import Optional

import typer
from github import Github, GithubException

app = typer.Typer()


@app.command()
def add_deploy_key(
    token: Optional[str] = None, user_repo: Optional[str] = None, repo_name: Optional[str] = None
):
    typer.echo("Starting to access GitHub.com... ")
    try:
        # using an access token
        g = Github(token)

        # I skipped a bunch of code to save space
        
        for key in repo.get_keys():
            if str(key.key) == str(pure_public_key):
                typer.echo(
                    "We found an existing public key in " + user_repo + ", so we're NOT adding it"
                )
                return
        rep_key = repo.create_key(
            "DigitalOcean for " + repo_name, current_public_key, read_only=True
        )
        if rep_key:
            typer.echo("Success with adding public key to repo in GitHub.com!")
            typer.echo("")
            typer.echo("The url to the deposited key is: " + rep_key.url)
        else:
            typer.echo("There's some issue when adding public key to repo in GitHub.com")
    except GithubException as e:
        typer.echo("There's some issue")
        typer.echo(str(e))
        return


if __name__ == "__main__":
    app()

The way I trigger is inside a bash script

output=$(python /opt/github-add-deploy-keys.py --token="$token" --user-repo="$user_repo" --repo-name="$repo_name")

It works. But sometimes it just hangs there without any output. And since it happens intermittently and not consistently, it's hard to debug.

I cannot be sure if it's a typer issue or a network issue or a GitHub.com issue. There's just nothing.

I want it to fail fast and often. I know there's a timeout and a retry for the GitHub object.

See https://pygithub.readthedocs.io/en/latest/github.html?highlight=retry#github.MainClass.Github

I wonder if I can do anything with these two parameters so at least I have a visual of knowing something is being done. I can add a lot of typer.echo statements but that would be extremely verbose.

I'm also unfamiliar with the retry object. I wish even if a retry is made, there will be some echo statements to tell me a retry is being attempted.

What can I try?

1 Answers

The timeout should prevent the github request to hang and retries should make it work but based on the documentation there is a default timeout already. Since this question is for debugging I would suggest using the logging python library to just the steps your script runs in a file. You can find a good logging tutorial here.

As for the logging style, since your case has a lot of unknowns I would log when the script starts, before "Create Key" step and after "Create Key" an maybe in case of errors. You can go over the log file when the script hangs.

You can create a bash script to run your script with timeout and get a notification if the program exits with non zero exit code and leave it to run over night:

timeout 5 python <yourscript>.py
Related