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

Socket Mode: Failed to connect (error: string argument without an encoding) w/ Azure App Service + aiohttp 3.11.2 #1202

Closed
jeremybeeman opened this issue Nov 17, 2024 · 5 comments
Assignees
Labels
area:async bug Something isn't working dependencies Pull requests that update a dependency file
Milestone

Comments

@jeremybeeman
Copy link

jeremybeeman commented Nov 17, 2024

Reproducible in:

The slack_bolt version

slack_bolt==1.21.2

Python runtime version

Python 3.12.2

OS info

Trying to run from an Azure App Service that is VNET integrated. All outbound connectivity is required to go via proxy.

OS version: Unix 5.15.164.1
64 bit system: True
64 bit process: True
Processor count: 1
Machine name: 2f7e55a2dc74
Instance id: 0e5af111c5b338ee855cdd761d032f2c022705d7e379fdcddb5270f11cfa1b68
Short instance id: 0e5af1
CLR version: 6.0.35
System directory:
Current working directory: /opt/Kudu
IIS command line: /opt/Kudu/Kudu.Services.Web.dll

(antenv) root@app-service:/home/site/wwwroot# cat /etc/os-release

PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"

NAME="Debian GNU/Linux"

VERSION_ID="11"

VERSION="11 (bullseye)"

VERSION_CODENAME=bullseye

ID=debian

HOME_URL=https://www.debian.org/

SUPPORT_URL=https://www.debian.org/support

BUG_REPORT_URL=https://bugs.debian.org/

(antenv) root@e9813d4e8a9f:/home/site/wwwroot# uname -r

5.15.164.1-1.cm2

Steps to reproduce:

Slack Bolt Socket Mode Snippet
proxy_url = os.getenv('PROXY_URL')

client = AsyncWebClient(

    token = os.getenv('SLACK_BOT_TOKEN'),

    proxy = proxy_url

)

app = AsyncApp(client = client)
handler = AsyncSocketModeHandler(

        app, os.getenv('SLACK_APP_TOKEN'),

        proxy = proxy_url
    )

handler_task = asyncio.create_task(handler.start_async())

The code snippet is used alongside a separate FastAPI instantiation and calls to a separate aiohttp Web Client.

Build and deploy the slack bolt code using the github actions deployment shown here: https://github.com/Azure/actions-workflow-samples/blob/master/AppService/python-webapp-on-azure.yml

Expected result:

To be able to create a socket connection between Slack and the app service.

Actual result:

The initial call to slack is made, and then the code then fails with a string encoding error with aiohttp (put XXXXX for some IDs).

2024-11-17T01:37:30.923152692Z Received the following response - status: 200, headers: 
{'Date': 'Sun, 17 Nov 2024 01:37:30 GMT', 'Server': 'Apache', 'Vary': 'Accept-Encoding', 'x-slack-req-id': 'XXXXX', 'x-content-type-options': 'nosniff', 'x-xss-protection': '0', 'x-robots-tag': 'noindex,nofollow', 'Pragma': 'no-cache', 'Cache-Control': 'private, no-cache, no-store, must-revalidate', 'Expires': 'Sat, 26 Jul 1997 05:00:00 GMT', 'Content-Type': 'application/json; charset=utf-8', 'x-accepted-oauth-scopes': 'connections:write', 'x-oauth-scopes': 'connections:write', 'Access-Control-Expose-Headers': 'x-slack-req-id, retry-after', 'Access-Control-Allow-Headers': 'slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, x-b3-sampled, x-b3-flags', 'strict-transport-security': 'max-age=31536000; includeSubDomains; preload', 'referrer-policy': 'no-referrer', 'x-slack-unique-id': 'XXXXX', 'x-slack-backend': 'r', 'Access-Control-Allow-Origin': '*', 'Via': '1.1 [slack-prod.tinyspeck.com](http://slack-prod.tinyspeck.com/), envoy-www-iad-pcrbflcw,envoy-edge-iad-fptlrjqw', 'Content-Encoding': 'gzip', 'Content-Length': '165', 'x-envoy-attempt-count': '1', 'x-envoy-upstream-service-time': '19', 'x-backend': 'main_normal main_canary_with_overflow main_control_with_overflow', 'x-server': 'slack-www-hhvm-main-iad-exxh', 'x-slack-shared-secret-outcome': 'no-match', 'x-edge-backend': 'envoy-www', 'x-geoname-id': 'notfound', 'x-slack-edge-shared-secret-outcome': 'no-match'}, body: {'ok': True, 'url': 'wss://wss-primary.slack.com/link/?ticket=481d5f77-0d56-402e-8472-73c552fefcd4&app_id=XXXXX'} 
2024-11-17T01:37:31.059929384Z INFO: [169.254.129.1:65493](http://169.254.129.1:65493/) - "GET /robots933456.txt HTTP/1.1" 404 Not Found 2024-11-17T01:37:31.070751719Z A new session (s_8500438350666) has been established 
2024-11-17T01:37:31.070787721Z Sending a ping message with the newly established connection (s_8500438350666)... 
2024-11-17T01:37:31.116712841Z Failed to connect (error: string argument without an encoding); Retrying... 
2024-11-17T01:37:31.116745843Z Traceback (most recent call last): 2024-11-17T01:37:31.116752044Z File "/home/site/wwwroot/antenv/lib/python3.12/site-packages/slack_sdk/socket_mode/aiohttp/__init__.py", line 390, in connect 2024-11-17T01:37:31.116757544Z await self.current_session.ping(f"sdk-ping-pong:{t}") 2024-11-17T01:37:31.116762544Z File "/home/site/wwwroot/antenv/lib/python3.12/site-packages/aiohttp/client_ws.py", line 229, in ping 2024-11-17T01:37:31.116767745Z await self._writer.send_frame(message, WSMsgType.PING) 2024-11-17T01:37:31.116772745Z File "/home/site/wwwroot/antenv/lib/python3.12/site-packages/aiohttp/_websocket/writer.py", line 136, in send_frame 2024-11-17T01:37:31.116777745Z message = bytearray(message)

Requirements

slack_bolt==1.21.2

requests==2.32.3

python-dotenv==1.0.1

slack_sdk==3.33.3

aiohttp==3.11.2

uvicorn==0.32.0

fastapi==0.115.5

@jeremybeeman
Copy link
Author

Update:

I was able to get the app to work again, but I did have to revert back to older versions of slack_bolt/slack_sdk

slack_bolt==1.20.1

requests==2.32.3

python-dotenv==1.0.1

slack_sdk==3.33.1

aiohttp==3.10.8

uvicorn==0.31.0

fastapi==0.115.0

@seratch seratch changed the title Socket Mode: Failed to connect (error: string argument without an encoding); Retrying.. forever Socket Mode: Failed to connect (error: string argument without an encoding) w/ Azure App Service + aiohttp Nov 18, 2024
@seratch seratch added question Further information is requested dependencies Pull requests that update a dependency file labels Nov 18, 2024
@seratch
Copy link
Member

seratch commented Nov 18, 2024

Hi @jeremybeeman, thank you for writing in.

I've reviewed all the changes between the versions you tried but we haven't made any changes to aiohttp modules.

aiohttp==3.10.8 -> aiohttp==3.11.2

I am thinking that your aiohttp minor version ugprade might cause the behavior difference.

/home/site/wwwroot/antenv/lib/python3.12/site-packages/aiohttp/_websocket/writer.py", line 136, in send_frame 2024-11-17T01:37:31.116777745Z message = bytearray(message)

The above error inside aiohttp library indeed could cause the "string argument without an encoding" error.

If there is a way for our slack-sdk to implement a workaround for this aiohttp 3.11 issue, we may do so in future releases. For now, please lock the aiohttp version to 3.10.x.

@seratch seratch changed the title Socket Mode: Failed to connect (error: string argument without an encoding) w/ Azure App Service + aiohttp Socket Mode: Failed to connect (error: string argument without an encoding) w/ Azure App Service + aiohttp 3.11.2 Nov 18, 2024
@dbeilin
Copy link

dbeilin commented Nov 18, 2024

I'm also having this issue since upgrading from aiohttp==3.10.x to aiohttp==3.11.x.

My main.py:

"""A Slack App to start a remote-dev instance on demand"""

import os
import asyncio
from dotenv import load_dotenv
from slack_bolt.async_app import AsyncApp
from slack_bolt.adapter.socket_mode.async_handler import AsyncSocketModeHandler
from logger import logger as log
from modules.handlers import SlackEventHandlers
from modules.helpers import SlackHelpers

async def main():
    """
    This function starts the Slack app
    """
    app = AsyncApp(
        token=os.environ.get("SLACK_BOT_TOKEN"),
        signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
    )
    event_handlers = SlackEventHandlers(app)
    slack_helpers = SlackHelpers()
    slack_helpers.check_required_env_vars()

    log.info("Starting Slack app")
    socket_mode_handler = AsyncSocketModeHandler(
        event_handlers.app, os.environ["SLACK_APP_TOKEN"]
    )
    await socket_mode_handler.start_async()


if __name__ == "__main__":
    asyncio.run(main())

Error:

{'time':'2024-11-18 17:37:28,661', 'level': 'INFO', 'message': 'Starting Slack app'}
{'time':'2024-11-18 17:37:29,468', 'level': 'INFO', 'message': 'A new session (s_271437660) has been established'}
{'time':'2024-11-18 17:37:29,468', 'level': 'ERROR', 'message': 'Failed to connect (error: string argument without an encoding); Retrying...'}
Traceback (most recent call last):
  File "/Users/dbeilin/.pyenv/versions/3.12.7/envs/slack_apps/lib/python3.12/site-packages/slack_sdk/socket_mode/aiohttp/__init__.py", line 390, in connect
    await self.current_session.ping(f"sdk-ping-pong:{t}")
  File "/Users/dbeilin/.pyenv/versions/3.12.7/envs/slack_apps/lib/python3.12/site-packages/aiohttp/client_ws.py", line 229, in ping
    await self._writer.send_frame(message, WSMsgType.PING)
  File "/Users/dbeilin/.pyenv/versions/3.12.7/envs/slack_apps/lib/python3.12/site-packages/aiohttp/_websocket/writer.py", line 136, in send_frame
    message = bytearray(message)

As a workaround, I was able to start my app using websockets:

from slack_bolt.adapter.socket_mode.websockets import AsyncSocketModeHandler

But there are some client differences, I see warnings in my logs while my app runs:

{'time':'2024-11-18 17:29:54,666', 'level': 'ERROR', 'message': 'Failed to check the current session or reconnect to the server (error: AttributeError, message: 'ClientConnection' object has no attribute 'closed', session: s_275359559)'}

I'll hold off upgrading to aiohttp 3.11 for now.

@seratch seratch added bug Something isn't working and removed question Further information is requested labels Nov 19, 2024
@seratch
Copy link
Member

seratch commented Nov 19, 2024

Received another feedback at slackapi/python-slack-sdk#1592 and it seems our aiohttp integration needs to adjust the code for this issue. New slack-sdk patch version will resolve it shortly

@seratch seratch added this to the 1.21.3 milestone Nov 19, 2024
@seratch seratch self-assigned this Nov 19, 2024
@seratch
Copy link
Member

seratch commented Nov 19, 2024

Thank you for being patient with this issue. Upgrading slack-sdk 3.33.4 should resolve it: https://github.com/slackapi/python-slack-sdk/releases/tag/v3.33.4

@seratch seratch closed this as completed Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:async bug Something isn't working dependencies Pull requests that update a dependency file
Projects
None yet
Development

No branches or pull requests

3 participants