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

GKE部署的Streamlit应用WebSocket自动频繁断开重连求助

问题:GKE上Streamlit应用WebSocket频繁断开重连,变量丢失

通过Cloud Build在Google Kubernetes Engine(GKE)上部署2个Streamlit应用后,遇到以下问题:

  • 每10秒左右自动触发一次WebSocket升级请求,连接建立后约10秒断开,随后自动向/_stcore/stream发起新请求,该过程持续循环,导致所有变量被清除
  • 此现象无需任何用户操作(点击、重运行或刷新页面)即可自动触发

在AWS环境中以非Kubernetes、非Docker的方式部署同款应用时,WebSocket连接稳定,仅需建立一次即可保持。

以下为无用户交互时自动重复生成的Pod日志,可见连接每15秒左右断开并重连:

2023-04-08 11:02:12.805 Script run finished successfully; removing expired entries from MessageCache (max_age=2)
2023-04-08 11:02:42.542 Runtime state: RuntimeState.ONE_OR_MORE_SESSIONS_CONNECTED -> RuntimeState.NO_SESSIONS_CONNECTED
2023-04-08 11:02:43.050 in-event <InotifyEvent: src_path=b'/app/main.py', wd=1, mask=IN_OPEN, cookie=0, name='main.py'>
2023-04-08 11:02:43.050 Watcher created for /app/main.py
2023-04-08 11:02:43.050 Runtime state: RuntimeState.NO_SESSIONS_CONNECTED -> RuntimeState.ONE_OR_MORE_SESSIONS_CONNECTED
2023-04-08 11:02:43.112 Received the following back message:
rerun_script {
  widget_states {
    widgets {
      id: "$$GENERATED_WIDGET_ID-03ad0d756f891356fcbde6a33ea79fe3-None"
      int_value: 0
    }
    widgets {
      id: "$$GENERATED_WIDGET_ID-7fb2e9aed3162f53d160852436451362-None"
      file_uploader_state_value {
      }
    }
  }
  page_script_hash: "5bc02cefb3ea9e27f1a6776eabd1935d"
}

2023-04-08 11:02:43.113 Beginning script thread
2023-04-08 11:02:43.113 Running script RerunData(query_string='', widget_states=widgets {
  id: "$$GENERATED_WIDGET_ID-03ad0d756f891356fcbde6a33ea79fe3-None"
  int_value: 0
}
widgets {
  id: "$$GENERATED_WIDGET_ID-7fb2e9aed3162f53d160852436451362-None"
  file_uploader_state_value {
  }
}
, page_script_hash='5bc02cefb3ea9e27f1a6776eabd1935d', page_name='')
2023-04-08 11:02:43.113 Disconnecting files for session with ID 1d33344d-ba21-4ed6-97d7-e93f1cf9fe80
2023-04-08 11:02:43.113 Sessions still active: dict_keys([])
2023-04-08 11:02:43.113 Files: 0; Sessions with files: 0
2023-04-08 11:02:43.114 in-event <InotifyEvent: src_path=b'/app/main.py', wd=1, mask=IN_OPEN, cookie=0, name='main.py'>
2023-04-08 11:02:43.148 Removing orphaned files...
2023-04-08 11:02:43.195 in-event <InotifyEvent: src_path=b'/app/.env', wd=1, mask=IN_OPEN, cookie=0, name='.env'>
2023-04-08 11:02:43.309 Script run finished successfully; removing expired entries from MessageCache (max_age=2)
2023-04-08 11:03:13.051 Runtime state: RuntimeState.ONE_OR_MORE_SESSIONS_CONNECTED -> RuntimeState.NO_SESSIONS_CONNECTED
2023-04-08 11:03:13.515 in-event <InotifyEvent: src_path=b'/app/main.py', wd=1, mask=IN_OPEN, cookie=0, name='main.py'>
2023-04-08 11:03:13.516 Watcher created for /app/main.py
2023-04-08 11:03:13.516 Runtime state: RuntimeState.NO_SESSIONS_CONNECTED -> RuntimeState.ONE_OR_MORE_SESSIONS_CONNECTED
2023-04-08 11:03:13.585 Received the following back message:
rerun_script {
  widget_states {
    widgets {
      id: "$$GENERATED_WIDGET_ID-03ad0d756f891356fcbde6a33ea79fe3-None"
      int_value: 0
    }
    widgets {
      id: "$$GENERATED_WIDGET_ID-7fb2e9aed3162f53d160852436451362-None"
      file_uploader_state_value {
      }
    }
  }
  page_script_hash: "5bc02cefb3ea9e27f1a6776eabd1935d"
}

2023-04-08 11:03:13.585 Beginning script thread
2023-04-08 11:03:13.585 Running script RerunData(query_string='', widget_states=widgets {
  id: "$$GENERATED_WIDGET_ID-03ad0d756f891356fcbde6a33ea79fe3-None"
  int_value: 0
}
widgets {
  id: "$$GENERATED_WIDGET_ID-7fb2e9aed3162f53d160852436451362-None"
  file_uploader_state_value {
  }
}
, page_script_hash='5bc02cefb3ea9e27f1a6776eabd1935d', page_name='')
2023-04-08 11:03:13.585 Disconnecting files for session with ID 1d33344d-ba21-4ed6-97d7-e93f1cf9fe80
2023-04-08 11:03:13.585 Sessions still active: dict_keys([])
2023-04-08 11:03:13.585 Files: 0; Sessions with files: 0
2023-04-08 11:03:13.586 in-event <InotifyEvent: src_path=b'/app/main.py', wd=1, mask=IN_OPEN, cookie=0, name='main.py'>
2023-04-08 11:03:13.620 Removing orphaned files...
2023-04-08 11:03:13.669 in-event <InotifyEvent: src_path=b'/app/.env', wd=1, mask=IN_OPEN, cookie=0, name='.env'>
2023-04-08 11:03:13.784 Script run finished successfully; removing expired entries from MessageCache (max_age=2)
2023-04-08 11:03:43.517 Runtime state: RuntimeState.ONE_OR_MORE_SESSIONS_CONNECTED -> RuntimeState.NO_SESSIONS_CONNECTED
2023-04-08 11:03:44.005 in-event <InotifyEvent: src_path=b'/app/main.py', wd=1, mask=IN_OPEN, cookie=0, name='main.py'>
2023-04-08 11:03:44.005 Watcher created for /app/main.py
2023-04-08 11:03:44.005 Runtime state: RuntimeState.NO_SESSIONS_CONNECTED -> RuntimeState.ONE_OR_MORE_SESSIONS_CONNECTED
2023-04-08 11:03:44.067 Received the following back message:
rerun_script {
  widget_states {
    widgets {
      id: "$$GENERATED_WIDGET_ID-03ad0d756f891356fcbde6a33ea79fe3-None"
      int_value: 0
    }
    widgets {
      id: "$$GENERATED_WIDGET_ID-7fb2e9aed3162f53d160852436451362-None"
      file_uploader_state_value {
      }
    }
  }
  page_script_hash: "5bc02cefb3ea9e27f1a6776eabd1935d"
}

2023-04-08 11:03:44.068 Beginning script thread
2023-04-08 11:03:44.068 Running script RerunData(query_string='', widget_states=widgets {
  id: "$$GENERATED_WIDGET_ID-03ad0d756f891356fcbde6a33ea79fe3-None"
  int_value: 0
}
widgets {
  id: "$$GENERATED_WIDGET_ID-7fb2e9aed3162f53d160852436451362-None"
  file_uploader_state_value {
  }
}
, page_script_hash='5bc02cefb3ea9e27f1a6776eabd1935d', page_name='')
2023-04-08 11:03:44.068 Disconnecting files for session with ID 1d33344d-ba21-4ed6-97d7-e93f1cf9fe80
2023-04-08 11:03:44.068 Sessions still active: dict_keys([])
2023-04-08 11:03:44.068 Files: 0; Sessions with files: 0
2023-04-08 11:03:44.069 in-event <InotifyEvent: src_path=b'/app/main.py', wd=1, mask=IN_OPEN, cookie=0, name='main.py'>
2023-04-08 11:03:44.102 Removing orphaned files...
2023-04-08 11:03:44.155 in-event <InotifyEvent: src_path=b'/app/.env', wd=1, mask=IN_OPEN, cookie=0, name='.env'>
2023-04-08 11:03:44.269 Script run finished successfully; removing expired entries from MessageCache (max_age=2)
2023-04-08 11:04:14.006 Runtime state: RuntimeState.ONE_OR_MORE_SESSIONS_CONNECTED -> RuntimeState.NO_SESSIONS_CONNECTED
2023-04-08 11:04:14.500 in-event <InotifyEvent: src_path=b'/app/main.py', wd=1, mask=IN_OPEN, cookie=0, name='main.py'>

期望GKE上的应用能拥有稳定的WebSocket连接。

内容的提问来源于stack exchange,提问作者Erce Can Bektüre

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 15:24:55