Python-slack-sdk: Multiple errors when querying the api with the same client, roughly simultaneously

Created on 21 May 2020  路  8Comments  路  Source: slackapi/python-slack-sdk

Description

Hello 馃憢 I noticed that if you start using slack client instance as a singleton in your app, you shall ready yourself for a lot of confusing exceptions. Personally, so far I've got:

aiohttp.client_exceptions.ClientConnectorSSLError: Cannot connect to host www.slack.com:443 ssl:default [[SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] decryption failed or bad record mac (_ssl.c:1076)]
IndexError: deque index out of range
aiohttp.client_exceptions.ClientPayloadError: Response payload is not completed

I think it would be awesome to document that it is preferred to create a fresh slack client instance per request. That is unless I'm missing something and it's a bug.

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

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

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

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

Most helpful comment

@kujon Thanks. The outcome is a bit different but it looks a similar issue with https://github.com/slackapi/python-slackclient/issues/497#issuecomment-617536013

As you mentioned, if you really want to use run_async=True, there is no other option apart from creating a dedicated WebClient instance per Flask request. See also: https://github.com/slackapi/python-slackclient/blob/master/integration_tests/samples/issues/issue_497.py

If you go with Flask, I don't see the necessity to heavily rely on aiohttp. If you use run_async=False in 2.6.0 (rc2 at this moment but the version will be released tomorrow!), you can reuse a single instance across your Flask app.

All 8 comments

Can you share more information? In particular, I'm curious about:

  • run_async=True or False (the default is False)
  • slackclient version
  • Python version
  • OS info
  • a minimum piece of code you use if possible

If you use run_async=False (the default) and version 2.x, could you try v2.6.0rc2 out?
https://pypi.org/project/slackclient/2.6.0rc2/

With this version, you won't be necessarily affected by aiohttp's behavior.

Sure thing @seratch!

  1. With run_async=True and a singleton client instance, I'm consistently getting:
Traceback (most recent call last):
  File "/Users/jakubkorzeniowski/.pyenv/versions/recognizer/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/jakubkorzeniowski/.pyenv/versions/recognizer/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/jakubkorzeniowski/.pyenv/versions/recognizer/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/jakubkorzeniowski/.pyenv/versions/recognizer/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Users/jakubkorzeniowski/.pyenv/versions/recognizer/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/jakubkorzeniowski/.pyenv/versions/recognizer/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/jakubkorzeniowski/vortexa_repos/recognizer/recognizer/api/breakdown.py", line 22, in breakdown
    to_timestamp=to_timestamp,
  File "/Users/jakubkorzeniowski/vortexa_repos/recognizer/recognizer/data/fetch.py", line 35, in fetch_messages
    _fetch_messages(client, channel_name, from_timestamp, to_timestamp)
  File "/Users/jakubkorzeniowski/vortexa_repos/recognizer/recognizer/data/utils.py", line 29, in messages_to_df
    for message in messages
  File "/Users/jakubkorzeniowski/vortexa_repos/recognizer/recognizer/data/utils.py", line 28, in <listcomp>
    [message_category(message), message_timestamp(message)]
  File "/Users/jakubkorzeniowski/vortexa_repos/recognizer/recognizer/data/fetch.py", line 12, in _fetch_messages
    client.conversations_list()["channels"], channel_name
TypeError: 'Task' object is not subscriptable
  1. slackclient==2.5.0
  2. Python 3.7.4
  3. macOS mojave, 10.14.6
  4. All requests more or less do client.conversations_list() to find the id of the channel by name, followed by client.conversations_history().

Bear in mind that creating a new WebClient instance for each request makes all of the above disappear.

Another piece of info is that I'm doing the following:

import asyncio
import nest_asyncio

nest_asyncio.apply()

This is to allow nested asyncio loops. If I don't do that, I'm getting the similar issue to what notebook users have experienced in the past: https://github.com/slackapi/python-slackclient/issues/429 (I'm running this code as part of a Flask application)

@kujon Thanks. The outcome is a bit different but it looks a similar issue with https://github.com/slackapi/python-slackclient/issues/497#issuecomment-617536013

As you mentioned, if you really want to use run_async=True, there is no other option apart from creating a dedicated WebClient instance per Flask request. See also: https://github.com/slackapi/python-slackclient/blob/master/integration_tests/samples/issues/issue_497.py

If you go with Flask, I don't see the necessity to heavily rely on aiohttp. If you use run_async=False in 2.6.0 (rc2 at this moment but the version will be released tomorrow!), you can reuse a single instance across your Flask app.

I think it would be awesome to document that it is preferred to create a fresh slack client instance per request.

I will update the document a bit. Thanks for the suggestion.

Thanks a lot for your help @seratch, that was rapid!

I've added a new part in FAQ regarding this. https://slack.dev/python-slackclient/faq.html#should-i-go-with-run-async

Let me close this issue now. Thanks for asking this question here!

Was this page helpful?
0 / 5 - 0 ratings