Skip to content

Automate browser based workflows with AI

fix: kill orphan subprocess on timeout and add jitter to Bitwarden CLI (#SKY-8053) (#5009)

LawyZheng176de0c77c39
diff --git a/skyvern/config.py b/skyvern/config.py
index 0df1910fe..a988b4d0b 100644
--- a/skyvern/config.py
+++ b/skyvern/config.py
@@ -155,6 +155,7 @@ class Settings(BaseSettings):
#####################
BITWARDEN_TIMEOUT_SECONDS: int = 60
BITWARDEN_MAX_RETRIES: int = 2
+ BITWARDEN_MAX_JITTER_SECONDS: float = 2.0
# task generation settings
PROMPT_CACHE_WINDOW_HOURS: int = 24
diff --git a/skyvern/forge/sdk/services/bitwarden.py b/skyvern/forge/sdk/services/bitwarden.py
index 2a42bc9a9..f917f0700 100644
--- a/skyvern/forge/sdk/services/bitwarden.py
+++ b/skyvern/forge/sdk/services/bitwarden.py
@@ -1,6 +1,7 @@
import asyncio
import json
import os
+import random
import re
from enum import IntEnum, StrEnum
from typing import Tuple
@@ -158,6 +159,15 @@ class RunCommandResult(BaseModel):
class BitwardenService:
+ @staticmethod
+ async def _apply_jitter() -> None:
+ """Apply random jitter delay to spread out concurrent Bitwarden CLI requests."""
+ max_jitter = settings.BITWARDEN_MAX_JITTER_SECONDS
+ if max_jitter > 0:
+ jitter = random.uniform(0, max_jitter)
+ LOG.debug("Applying Bitwarden jitter delay", jitter_seconds=round(jitter, 2))
+ await asyncio.sleep(jitter)
+
@staticmethod
async def run_command(
command: list[str], additional_env: dict[str, str] | None = None, timeout: int = 60
@@ -171,6 +181,7 @@ class BitwardenService:
if additional_env:
env.update(additional_env) # Update with any additional environment variables
+ shell_subprocess = None
try:
async with asyncio.timeout(timeout):
shell_subprocess = await asyncio.create_subprocess_shell(
@@ -186,7 +197,19 @@ class BitwardenService:
returncode=shell_subprocess.returncode,
)
except asyncio.TimeoutError as e:
- LOG.error(f"Bitwarden command timed out after {timeout} seconds", exc_info=True)
+ LOG.error(
+ "Bitwarden command timed out",
+ timeout_seconds=timeout,
+ command=command[0:2],
+ exc_info=True,
+ )
+ if shell_subprocess and shell_subprocess.returncode is None:
+ LOG.info("Killing timed-out Bitwarden subprocess", pid=shell_subprocess.pid)
+ try:
+ shell_subprocess.kill()
+ await shell_subprocess.wait()
+ except ProcessLookupError:
+ pass
raise e
@staticmethod
@@ -227,6 +250,7 @@ class BitwardenService:
if item_id and not is_uuid(item_id):
raise BitwardenGetItemError(f"Invalid item ID: {item_id}. Check if the item ID is correct")
+ await BitwardenService._apply_jitter()
for i in range(max_retries):
# FIXME: just simply double the timeout for the second try. maybe a better backoff policy when needed
timeout = (i + 1) * timeout
@@ -421,6 +445,8 @@ class BitwardenService:
"""
if not bw_organization_id and bw_collection_ids and collection_id not in bw_collection_ids:
raise BitwardenAccessDeniedError()
+ if not fail_reasons:
+ await BitwardenService._apply_jitter()
try:
async with asyncio.timeout(timeout):
return await BitwardenService._get_sensitive_information_from_identity(
@@ -705,6 +731,8 @@ class BitwardenService:
if not is_uuid(item_id):
raise BitwardenGetItemError(f"Invalid item ID: {item_id}. Check if the item ID is correct")
+ if not fail_reasons:
+ await BitwardenService._apply_jitter()
try:
async with asyncio.timeout(settings.BITWARDEN_TIMEOUT_SECONDS):
return await BitwardenService._get_credit_card_data(