You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Airflow 2.5创建Boto3 S3客户端实例任务卡住无报错,如何调试?

Boto3客户端创建时Airflow任务无响应排查

问题现象

尝试创建Boto3客户端实例(例如s3 = boto3.client('s3'))时,Airflow任务卡住,既不结束、超时也不抛出错误。任务处于运行状态(绿色进度条),而非调度/排队状态。且仅在Airflow 2.5版本中出现此问题,2.1.2版本可正常运行。

疑问

  1. 如何调试该问题?
  2. 需要确保哪些环境变量/文件存在?

任务实例日志

*** Reading local file: ...
...
--------------------------------------------------------------------------------
[2023-04-03, 10:13:15 IST] {taskinstance.py:1283} INFO - Starting attempt 1 of 1
...
[2023-04-03, 10:13:15 IST] {task_command.py:388} INFO - Running <TaskInstance: check_boto3_connection.check manual__2023-04-03T04:43:09.385235+00:00 [running]> on ...
[2023-04-03, 10:13:15 IST] {taskinstance.py:1509} INFO - Exporting the following env vars:
AIRFLOW_CTX_DAG_OWNER=***
AIRFLOW_CTX_DAG_ID=check_boto3_connection
AIRFLOW_CTX_TASK_ID=check
...
AIRFLOW_CTX_TRY_NUMBER=1
...
[2023-04-03, 10:13:15 IST] {logging_mixin.py:137} INFO - Creating Boto3 client...

完整DAG代码

import datetime

import boto3
import botocore.client
from airflow.decorators import dag, task


@dag(schedule=None, start_date=datetime.datetime(2023, 4, 1), catchup=False)
def check_boto3_connection():
    check()


@task
def check():
    print('Creating Boto3 client...')
    s3 = boto3.client(
        's3',
        config=botocore.client.Config(
            connect_timeout=5,
            retries={'max_attempts': 0},
        ),
    )
    print('S3 client:', s3)


check_boto3_connection()

调试方案

1. 启用详细日志追踪

  • 在任务代码中添加Boto3调试日志,捕获底层请求细节:
    import logging
    boto3.set_stream_logger('', logging.DEBUG)
    
  • 查看Airflow worker的stderr日志,任务卡住时的隐藏输出可能在标准错误流中
  • 检查worker系统级日志(如/var/log/airflow/worker.log),排查资源耗尽、死锁或依赖库崩溃情况

2. 直接在worker环境中测试

  • 登录运行任务的worker节点,进入Airflow的Python环境手动执行客户端创建代码,模拟任务场景,观察是否同样卡住及终端输出

3. 对比依赖版本差异

  • 分别在Airflow 2.1.2和2.5环境中查看boto3、botocore版本:
    pip list | grep -E "boto3|botocore"
    
  • 尝试将新版本环境的依赖降级到旧版本中可用的版本,验证是否为版本兼容问题

4. 排查网络与权限阻塞

  • 测试worker到S3端点的连通性:
    nc -zv s3.amazonaws.com 443
    
  • 用AWS CLI验证权限与连通性:
    aws s3 ls
    
    若CLI也卡住,说明问题出在网络或IAM权限层面,而非代码本身

5. 强化超时与异常捕获

  • 修改任务代码,添加读取超时并捕获所有异常:
    @task
    def check():
        print('Creating Boto3 client...')
        try:
            s3 = boto3.client(
                's3',
                config=botocore.client.Config(
                    connect_timeout=5,
                    read_timeout=5,  # 新增读取超时
                    retries={'max_attempts': 0},
                ),
            )
            print('S3 client:', s3)
        except Exception as e:
            print(f"Error creating client: {str(e)}")
            import traceback
            traceback.print_exc()
    

需确保的环境变量与文件

1. AWS凭证相关

  • 环境变量:AWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY(使用长期凭证时)
  • 环境变量:AWS_SESSION_TOKEN(使用临时凭证如IAM角色时)
  • 凭证文件:worker用户家目录下的~/.aws/credentials
  • 配置文件:worker用户家目录下的~/.aws/config(可选,用于指定区域、端点等)

2. Airflow运行环境

  • 确保AIRFLOW__CORE__EXECUTOR等配置未过度限制worker资源,CeleryExecutor需检查并发数设置
  • 使用IAM角色关联的worker(如ECS/EKS实例),需确保实例拥有正确IAM权限,且元数据服务http://169.254.169.254可访问

3. 依赖库完整性

  • 确保boto3和botocore在worker环境中完整安装,无损坏:
    pip install --force-reinstall boto3 botocore
    

内容的提问来源于stack exchange,提问作者m01010011

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 21:57:50