slackapi/python-slack-sdk

Unpredictable behaviour using .files_upload_v2

donneprosus opened this issue · 3 comments

Hello,

I am using the sdk to send messages with file attachments to slack threads. My bot works across multiple spaces but in some spaces the files_upload_v2 has started "failing" in a seemingly random way.

Using this API can results in three states:
A request with the same content to the same thread and same channel will (seemingly randomly) switch between these 3 states. State 3 is the biggest issue I am facing.

  1. The file and message are sent successfully
  2. The file and message send but the mime type is not populated in the "files" property of the response. If client.files_info(file=file['id']).data['file']["mimetype"] is called afterwards the mime type will be populated.
  3. The file and message are not sent and there is no exception thrown. Waiting does not result in the message appearing eventually. In this case the mime type is also not in the initial response from client.files_upload_v2.

In all cases the permalink is populated.
I have shared code that produces states 1 and 3, although when it will reach either stage is unknown.

The Slack SDK version

slack-sdk==3.22.0

Python runtime version

3.10.13

OS info

ProductName:		macOS
ProductVersion:		13.3
BuildVersion:		22E252
Darwin Kernel Version 22.4.0: Mon Mar  6 20:59:58 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6020

Steps to reproduce:

I run this code in a jupyter notebook

SLACK_BOT_TOKEN=""
i=0

import io
from slack_sdk.web.client import WebClient

client = WebClient(token=SLACK_BOT_TOKEN)
f_bytes = io.BytesIO(f"test string number {i}, {i}".encode())

response = client.files_upload_v2(
channel="",
file=f_bytes,
filename=f"file-upload-v2-{i}.csv",
title=f"file-upload-v2-{i}{i}.csv",
initial_comment="File generated for testing purposes",
thread_ts="",
    
)
response["files"]

Expected result:

I would expect that the method always successfully sends a message with the file attached to the slack thread or raises an error.

Actual result:

Sometimes this works as expected the message is shared with the file contents but frequently there is no message or file sent to the thread and no error is raised. Besides the mime type being missing depending on which outcome I get, the other defining feature is the that the shares dictionary in response["files"] property is empty when the message and file do not appear in the slack thread. I cant see anything else to indicate that it has failed.

I can share specific references to threads and files through direct message if needed.

Hi @donneprosus, thanks for asking the question.

The file and message send but the mime type is not populated in the "files" property of the response. If client.files_info(file=file['id']).data['file']["mimetype"] is called afterwards the mime type will be populated.

The v2 method can take some time to finalize full file metadata, so the initial metadata could be still incomplete. This async nature is the reason why the platform had to release v2 method to resolve the runtime performance issue with v1. Mimetype and shares are the ones that may need to be fetched later on the user side. To deal with this, please call files.info API method in addition to files.upload v2 method like you're already doing.

The file and message are not sent and there is no exception thrown. Waiting does not result in the message appearing eventually. In this case the mime type is also not in the initial response from client.files_upload_v2.

I cannot guess the exact reason why this situation arised to you, but if the SDK / platform can provide more information for finding the cause of it out, we will improve it in future releases. If you haven't enabled debug-level logging yet, turning it out may help you identify the reason of the unexpected behavior:

import logging
logging.basicConfig(level=logging.DEBUG)

I hope this clarifies.