Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

42%
+1 −2
Q&A Program freezing in `post_comments_to_lemmy` function due to potential infinite loop

I'm experiencing an issue where my program appears to freeze when executing the post_comments_to_lemmy function. This function fetches comments from the GitHub API and posts them to a Lemmy instanc...

1 answer  ·  posted 1y ago by ShadowsRanger‭  ·  last activity 1y ago by __blackjack__‭

Question python
#4: Post edited by user avatar ShadowsRanger‭ · 2023-07-14T10:37:34Z (over 1 year ago)
  • I'm experiencing an issue where my program appears to freeze when executing the `post_comments_to_lemmy` function. This function fetches comments from the GitHub API and posts them to a Lemmy instance. However, it seems that there might be a potential infinite loop within the function that causes the program to hang indefinitely.
  • I've noticed that the code includes a check within the `for` loop that skips the iteration if `comment_dict` is an instance of a string (`str`). However, if all the `comment_dict` objects are strings, the loop may continue indefinitely, resulting in the program appearing to freeze.
  • I'm looking for guidance on how to fix this issue and prevent the program from freezing.
  • For more context [here](https://lemm.ee/post/1094540) is the script.
  • ```python
  • def post_comments_to_lemmy(
  • lemmy: Any, post_id: int, github_repo: str, issue: Issue
  • ) -> None:
  • logging.info(
  • f"Posting comments from issue #{issue.number} to Lemmy post {LEMMY_INSTANCE_URL}/post/{post_id}"
  • )
  • try:
  • comments_url: str = (
  • f"{GITHUB_API_BASE}/repos/{github_repo}/issues/{issue.number}/comments"
  • )
  • comments: list[dict[str, Any]] = fetch_github_data(comments_url)
  • for comment_dict in comments:
  • if isinstance(comment_dict, str):
  • continue
  • comment = Comment(comment_dict, issue.number)
  • post_comment_to_lemmy(lemmy, github_repo, comment, post_id, issue.number)
  • except Exception as e:
  • logging.exception(f"Error posting comments to lemmy post {post_id}\n{e}")
  • raise
  • def fetch_github_data(url):
  • try:
  • headers = {"Accept": "application/vnd.github+json"}
  • response = requests.get(url, headers=headers)
  • response.raise_for_status()
  • time.sleep(DELAY)
  • logging.info(f"Fetched data from {url}")
  • return response.json()
  • except requests.exceptions.RequestException as e:
  • logging.exception(f"Error fetching data from {url}\n{e}")
  • raise
  • [INFO]:2023-07-14 10:40:46,748:root [github_lemmy_issue_reposter.py:275 - post_comments_to_lemmy()] Posting comments from issue #1889 to Lemmy post https://lemm.ee/post/1111239
  • [INFO]:2023-07-14 10:40:48,035:root [github_lemmy_issue_reposter.py:93 - fetch_github_data()] Fetched data from https://api.github.com/repos/LemmyNet/lemmy-ui/issues/1889/comments
  • ```
  • I'm experiencing an issue where my program appears to freeze when executing the `post_comments_to_lemmy` function. This function fetches comments from the GitHub API and posts them to a Lemmy instance. However, it seems that there might be a potential infinite loop within the function that causes the program to hang indefinitely.
  • I've noticed that the code includes a check within the `for` loop that skips the iteration if `comment_dict` is an instance of a string (`str`). However, if all the `comment_dict` objects are strings, the loop may continue indefinitely, resulting in the program appearing to freeze.
  • I'm looking for guidance on how to fix this issue and prevent the program from freezing.
  • For more context [here](https://git.disroot.org/hirrolot19/lemmy-scripts/src/commit/ba391c99be1e49d4eb154a822ce2da2f1cfd5771/github_lemmy_issue_reposter/github_lemmy_issue_reposter.py) is the script.
  • ```python
  • def post_comments_to_lemmy(
  • lemmy: Any, post_id: int, github_repo: str, issue: Issue
  • ) -> None:
  • logging.info(
  • f"Posting comments from issue #{issue.number} to Lemmy post {LEMMY_INSTANCE_URL}/post/{post_id}"
  • )
  • try:
  • comments_url: str = (
  • f"{GITHUB_API_BASE}/repos/{github_repo}/issues/{issue.number}/comments"
  • )
  • comments: list[dict[str, Any]] = fetch_github_data(comments_url)
  • for comment_dict in comments:
  • if isinstance(comment_dict, str):
  • continue
  • comment = Comment(comment_dict, issue.number)
  • post_comment_to_lemmy(lemmy, github_repo, comment, post_id, issue.number)
  • except Exception as e:
  • logging.exception(f"Error posting comments to lemmy post {post_id}\n{e}")
  • raise
  • def fetch_github_data(url):
  • try:
  • headers = {"Accept": "application/vnd.github+json"}
  • response = requests.get(url, headers=headers)
  • response.raise_for_status()
  • time.sleep(DELAY)
  • logging.info(f"Fetched data from {url}")
  • return response.json()
  • except requests.exceptions.RequestException as e:
  • logging.exception(f"Error fetching data from {url}\n{e}")
  • raise
  • [INFO]:2023-07-14 10:40:46,748:root [github_lemmy_issue_reposter.py:275 - post_comments_to_lemmy()] Posting comments from issue #1889 to Lemmy post https://lemm.ee/post/1111239
  • [INFO]:2023-07-14 10:40:48,035:root [github_lemmy_issue_reposter.py:93 - fetch_github_data()] Fetched data from https://api.github.com/repos/LemmyNet/lemmy-ui/issues/1889/comments
  • ```
#3: Post edited by user avatar ShadowsRanger‭ · 2023-07-14T10:07:13Z (over 1 year ago)
  • The program seems to have frozen. It may be stuck in an infinite loop.
  • In the `post_comments_to_lemmy` function, the comments fetched from the GitHub API are iterated over in a `for` loop. However, there is a check within the loop that skips the iteration if `comment_dict` is an instance of a string (`str`). If all the `comment_dict` objects are strings, the loop may continue indefinitely, resulting in the program appearing to freeze.
  • How can I fix this issue?
  • For more context [here](https://lemm.ee/post/1094540) is the script.
  • ```python
  • def post_comments_to_lemmy(
  • lemmy: Any, post_id: int, github_repo: str, issue: Issue
  • ) -> None:
  • logging.info(
  • f"Posting comments from issue #{issue.number} to Lemmy post {LEMMY_INSTANCE_URL}/post/{post_id}"
  • )
  • try:
  • comments_url: str = (
  • f"{GITHUB_API_BASE}/repos/{github_repo}/issues/{issue.number}/comments"
  • )
  • comments: list[dict[str, Any]] = fetch_github_data(comments_url)
  • for comment_dict in comments:
  • if isinstance(comment_dict, str):
  • continue
  • comment = Comment(comment_dict, issue.number)
  • post_comment_to_lemmy(lemmy, github_repo, comment, post_id, issue.number)
  • except Exception as e:
  • logging.exception(f"Error posting comments to lemmy post {post_id}\n{e}")
  • raise
  • def fetch_github_data(url):
  • try:
  • headers = {"Accept": "application/vnd.github+json"}
  • response = requests.get(url, headers=headers)
  • response.raise_for_status()
  • time.sleep(DELAY)
  • logging.info(f"Fetched data from {url}")
  • return response.json()
  • except requests.exceptions.RequestException as e:
  • logging.exception(f"Error fetching data from {url}\n{e}")
  • raise
  • [INFO]:2023-07-14 10:40:46,748:root [github_lemmy_issue_reposter.py:275 - post_comments_to_lemmy()] Posting comments from issue #1889 to Lemmy post https://lemm.ee/post/1111239
  • [INFO]:2023-07-14 10:40:48,035:root [github_lemmy_issue_reposter.py:93 - fetch_github_data()] Fetched data from https://api.github.com/repos/LemmyNet/lemmy-ui/issues/1889/comments
  • ```
  • I'm experiencing an issue where my program appears to freeze when executing the `post_comments_to_lemmy` function. This function fetches comments from the GitHub API and posts them to a Lemmy instance. However, it seems that there might be a potential infinite loop within the function that causes the program to hang indefinitely.
  • I've noticed that the code includes a check within the `for` loop that skips the iteration if `comment_dict` is an instance of a string (`str`). However, if all the `comment_dict` objects are strings, the loop may continue indefinitely, resulting in the program appearing to freeze.
  • I'm looking for guidance on how to fix this issue and prevent the program from freezing.
  • For more context [here](https://lemm.ee/post/1094540) is the script.
  • ```python
  • def post_comments_to_lemmy(
  • lemmy: Any, post_id: int, github_repo: str, issue: Issue
  • ) -> None:
  • logging.info(
  • f"Posting comments from issue #{issue.number} to Lemmy post {LEMMY_INSTANCE_URL}/post/{post_id}"
  • )
  • try:
  • comments_url: str = (
  • f"{GITHUB_API_BASE}/repos/{github_repo}/issues/{issue.number}/comments"
  • )
  • comments: list[dict[str, Any]] = fetch_github_data(comments_url)
  • for comment_dict in comments:
  • if isinstance(comment_dict, str):
  • continue
  • comment = Comment(comment_dict, issue.number)
  • post_comment_to_lemmy(lemmy, github_repo, comment, post_id, issue.number)
  • except Exception as e:
  • logging.exception(f"Error posting comments to lemmy post {post_id}\n{e}")
  • raise
  • def fetch_github_data(url):
  • try:
  • headers = {"Accept": "application/vnd.github+json"}
  • response = requests.get(url, headers=headers)
  • response.raise_for_status()
  • time.sleep(DELAY)
  • logging.info(f"Fetched data from {url}")
  • return response.json()
  • except requests.exceptions.RequestException as e:
  • logging.exception(f"Error fetching data from {url}\n{e}")
  • raise
  • [INFO]:2023-07-14 10:40:46,748:root [github_lemmy_issue_reposter.py:275 - post_comments_to_lemmy()] Posting comments from issue #1889 to Lemmy post https://lemm.ee/post/1111239
  • [INFO]:2023-07-14 10:40:48,035:root [github_lemmy_issue_reposter.py:93 - fetch_github_data()] Fetched data from https://api.github.com/repos/LemmyNet/lemmy-ui/issues/1889/comments
  • ```
#2: Post edited by user avatar ShadowsRanger‭ · 2023-07-14T10:03:45Z (over 1 year ago)
  • Fixing Possible Infinite Loop Issue in the `post_comments_to_lemmy` Function
  • Program freezing in `post_comments_to_lemmy` function due to potential infinite loop
#1: Initial revision by user avatar ShadowsRanger‭ · 2023-07-14T09:37:39Z (over 1 year ago)
Fixing Possible Infinite Loop Issue in the `post_comments_to_lemmy` Function
The program seems to have frozen. It may be stuck in an infinite loop.

In the `post_comments_to_lemmy` function, the comments fetched from the GitHub API are iterated over in a `for` loop. However, there is a check within the loop that skips the iteration if `comment_dict` is an instance of a string (`str`). If all the `comment_dict` objects are strings, the loop may continue indefinitely, resulting in the program appearing to freeze.

How can I fix this issue?

For more context [here](https://lemm.ee/post/1094540) is the script.

```python
def post_comments_to_lemmy(
    lemmy: Any, post_id: int, github_repo: str, issue: Issue
) -> None:
    logging.info(
        f"Posting comments from issue #{issue.number} to Lemmy post {LEMMY_INSTANCE_URL}/post/{post_id}"
    )
    try:
        comments_url: str = (
            f"{GITHUB_API_BASE}/repos/{github_repo}/issues/{issue.number}/comments"
        )
        comments: list[dict[str, Any]] = fetch_github_data(comments_url)
        for comment_dict in comments:
            if isinstance(comment_dict, str):
                continue
            comment = Comment(comment_dict, issue.number)
            post_comment_to_lemmy(lemmy, github_repo, comment, post_id, issue.number)
    except Exception as e:
        logging.exception(f"Error posting comments to lemmy post {post_id}\n{e}")
        raise

def fetch_github_data(url):
    try:
        headers = {"Accept": "application/vnd.github+json"}
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        time.sleep(DELAY)
        logging.info(f"Fetched data from {url}")
        return response.json()
    except requests.exceptions.RequestException as e:
        logging.exception(f"Error fetching data from {url}\n{e}")
        raise

[INFO]:2023-07-14 10:40:46,748:root [github_lemmy_issue_reposter.py:275 - post_comments_to_lemmy()] Posting comments from issue #1889 to Lemmy post https://lemm.ee/post/1111239
[INFO]:2023-07-14 10:40:48,035:root [github_lemmy_issue_reposter.py:93 - fetch_github_data()] Fetched data from https://api.github.com/repos/LemmyNet/lemmy-ui/issues/1889/comments
```