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

asyncio-based Socket Mode client improvements #1117

Merged
merged 5 commits into from Sep 18, 2021

Conversation

seratch
Copy link
Member

@seratch seratch commented Sep 17, 2021

Summary

This pull request applies a few improvements to asyncio-based Socket Mode clients.

  1. Add initial ping request to connect() method in AIOHTTP based client

This pull request adds a missing change in the pull request #1112

Like the built-in client implementation does, sending our own ping message to check if the connection is healthy can make the session check logic more accurate. If the connection is working, the client instance receives a new PONG message immediately and update the self.last_ping_pong_time with the timestamp of the latest trial.

This change can improve the situation described in this comment: #1110 (comment) (= the situation where the current connection is determined as stale 10 seconds after the last reconnection).

  1. Improve the monitoring task not to access self.current_session directly

For both AIOHTTP and websockets implementations, this pull request improves the internals of monitor_current_session and receive_messages not to access the instance field self.current_session. Instead of that, now these methods accesses only the local variable set when the method execution starts. While doing more tests with these clients, I found that accessing shared objects from tasks can cause unexpected race conditions and invalid data reference. The changes in this PR will make the executions of these methods much safer.

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

  • slack_sdk.web.WebClient (sync/async) (Web API client)
  • slack_sdk.webhook.WebhookClient (sync/async) (Incoming Webhook, response_url sender)
  • slack_sdk.socket_mode (Socket Mode client)
  • slack_sdk.signature (Request Signature Verifier)
  • slack_sdk.oauth (OAuth Flow Utilities)
  • slack_sdk.models (UI component builders)
  • slack_sdk.scim (SCIM API client)
  • slack_sdk.audit_logs (Audit Logs API client)
  • slack_sdk.rtm_v2 (RTM client)
  • /docs-src (Documents, have you run ./scripts/docs.sh?)
  • /docs-src-v2 (Documents, have you run ./scripts/docs-v2.sh?)
  • /tutorial (PythOnBoardingBot tutorial)
  • tests/integration_tests (Automated tests for this library)

Requirements (place an x in each [ ])

  • 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 run python3 -m venv .venv && source .venv/bin/activate && ./scripts/run_validation.sh after making the changes.

@seratch seratch added bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented Version: 3x socket-mode labels Sep 17, 2021
@seratch seratch added this to the 3.12.0 milestone Sep 17, 2021
@seratch seratch self-assigned this Sep 17, 2021
@codecov
Copy link

codecov bot commented Sep 17, 2021

Codecov Report

Merging #1117 (0bdef81) into main (9b3cf06) will increase coverage by 3.11%.
The diff coverage is 78.04%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1117      +/-   ##
==========================================
+ Coverage   84.96%   88.08%   +3.11%     
==========================================
  Files         110      110              
  Lines       10572    10681     +109     
==========================================
+ Hits         8983     9408     +425     
+ Misses       1589     1273     -316     
Impacted Files Coverage Δ
slack_sdk/socket_mode/async_client.py 76.92% <70.00%> (+5.75%) ⬆️
slack_sdk/socket_mode/aiohttp/__init__.py 81.88% <73.41%> (+38.47%) ⬆️
slack_sdk/socket_mode/websockets/__init__.py 90.18% <84.00%> (+33.51%) ⬆️
slack_sdk/socket_mode/request.py 75.00% <0.00%> (+3.12%) ⬆️
slack_sdk/socket_mode/client.py 76.99% <0.00%> (+4.42%) ⬆️
slack_sdk/socket_mode/builtin/client.py 90.50% <0.00%> (+15.82%) ⬆️
slack_sdk/socket_mode/builtin/internals.py 72.64% <0.00%> (+24.21%) ⬆️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9b3cf06...0bdef81. Read the comment docs.

async def is_connected(self) -> bool:
return (
not self.closed
and not self.stale
and self.current_session is not None
and not self.current_session.closed
and not await self.is_ping_pong_failing()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the ping-pong validation logic to is_ping_pong_failing method and added it to is_connected() method

@seratch seratch changed the title Add initial ping right after establishing a new connection in AIOHTTP-based SocketModeClient Add internal improvements to AIOHTTP-based SocketModeClient Sep 17, 2021
@seratch seratch modified the milestones: 3.12.0, 3.11.1 Sep 17, 2021
@seratch seratch changed the title Add internal improvements to AIOHTTP-based SocketModeClient asyncio-based Socket Mode client improvements Sep 18, 2021
@seratch
Copy link
Member Author

seratch commented Sep 18, 2021

I've done testing with these changes and found that the improvements here can eliminate (at least mitigate) some race conditions / invalid data references while reconnection. Let me merge this and release a new patch version including the changes.

@seratch seratch merged commit 9fd264f into slackapi:main Sep 18, 2021
@seratch seratch deleted the initial-ping-aiohttp branch September 18, 2021 04:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented socket-mode Version: 3x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant