Skip to content

Bad file descriptor error using Celery #669

@k0t3n

Description

@k0t3n

Description

postMessage method fails using Celery

What type of issue is this? (place an x in one of the [ ])

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • testing related
  • discussion

Requirements (place an x in each of the [ ])

  • I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

Reproducible in:

slackclient version: 2.5.0

python version: 3.7

OS version(s): MacOS 10.15.4

Steps to reproduce:

  1. Create a simple Celery postMessage task
  2. Run it

Expected result:

Message is sent

Actual result:

Message sending fails

Attachments:

Task example

@app.task
def send_slack_notifications():
    notifications = Notification.objects.filter(status=Notification.NEW, type=Notification.SLACK)

    client = WebClient(token=settings.SLACK_API_TOKEN, run_async=False)
    for notification in notifications:
        try:
            client.chat_postMessage(
                channel=f'#{notification.receiver}',
                text="Hello world!",
            )
            notification.set_success()
        except SlackApiError as e:
            notification.set_failed()
            log.exception(f"An error occurred during slack message send - {e.response['error']}")

Error message:

[2020-05-04 10:53:41,480: ERROR/ForkPoolWorker-1] Task src.apps.notifications.tasks.send_slack_notifications[acfcb697-79f5-4ffd-958f-1139f1c3f5d0] raised unexpected: OSError(9, 'Bad file descriptor')
Traceback (most recent call last):
  File "/Users/k0t3n/.local/share/virtualenvs/exchanger-a9eTqm1t/lib/python3.7/site-packages/celery/app/trace.py", line 385, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/Users/k0t3n/.local/share/virtualenvs/exchanger-a9eTqm1t/lib/python3.7/site-packages/celery/app/trace.py", line 650, in __protected_call__
    return self.run(*args, **kwargs)
  File "/Users/k0t3n/PycharmProjects/exchanger/src/apps/notifications/tasks.py", line 22, in send_slack_notifications
    text="Hello world!",
  File "/Users/k0t3n/.local/share/virtualenvs/exchanger-a9eTqm1t/lib/python3.7/site-packages/slack/web/client.py", line 660, in chat_postMessage
    return self.api_call("chat.postMessage", json=kwargs)
  File "/Users/k0t3n/.local/share/virtualenvs/exchanger-a9eTqm1t/lib/python3.7/site-packages/slack/web/base_client.py", line 174, in api_call
    return self._event_loop.run_until_complete(future)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 570, in run_until_complete
    self.run_forever()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 538, in run_forever
    self._run_once()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 1746, in _run_once
    event_list = self._selector.select(timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/selectors.py", line 558, in select
    kev_list = self._selector.control(None, max_ev, timeout)
OSError: [Errno 9] Bad file descriptor

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions