Skip to content

Commit

Permalink
Changed timeouts to milliseconds and timedelta for python
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Jun 19, 2024
1 parent dbe5df9 commit 8ecec15
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
3 changes: 2 additions & 1 deletion hogvm/python/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import timedelta
import sys
import json
from .execute import execute_bytecode
Expand All @@ -18,6 +19,6 @@
code = file.read()
code = json.loads(code)

response = execute_bytecode(code, globals=None, timeout=5, team=None, debug=debug)
response = execute_bytecode(code, globals=None, timeout=timedelta(seconds=5), team=None, debug=debug)
for line in response.stdout:
print(line) # noqa: T201
7 changes: 4 additions & 3 deletions hogvm/python/execute.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import timedelta
import re
import time
from copy import deepcopy
Expand Down Expand Up @@ -26,7 +27,7 @@ def execute_bytecode(
bytecode: list[Any],
globals: Optional[dict[str, Any]] = None,
functions: Optional[dict[str, Callable[..., Any]]] = None,
timeout=5,
timeout=timedelta(seconds=5),
team: Optional["Team"] = None,
debug=False,
) -> BytecodeResult:
Expand Down Expand Up @@ -60,8 +61,8 @@ def pop_stack():
return BytecodeResult(result=None, stdout=stdout, bytecode=bytecode)

def check_timeout():
if time.time() - start_time > timeout and not debug:
raise HogVMException(f"Execution timed out after {timeout} seconds. Performed {ops} ops.")
if time.time() - start_time > timeout.seconds and not debug:
raise HogVMException(f"Execution timed out after {timeout.seconds} seconds. Performed {ops} ops.")

while True:
ops += 1
Expand Down
13 changes: 8 additions & 5 deletions hogvm/typescript/src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ASYNC_STL, STL } from './stl/stl'
import { convertHogToJS, convertJSToHog, getNestedValue, like, setNestedValue } from './utils'

const DEFAULT_MAX_ASYNC_STEPS = 100
const DEFAULT_TIMEOUT = 5 // seconds
const DEFAULT_TIMEOUT_MS = 5000 // ms

export interface VMState {
/** Bytecode running in the VM */
Expand All @@ -25,10 +25,13 @@ export interface VMState {
}

export interface ExecOptions {
/** Global variables to be passed into the function */
globals?: Record<string, any>
functions?: Record<string, (...args: any[]) => any>
asyncFunctions?: Record<string, (...args: any[]) => Promise<any>>
/** Timeout in milliseconds */
timeout?: number
/** Max number of async function that can happen. When reached the function will throw */
maxAsyncSteps?: number
}

Expand Down Expand Up @@ -66,7 +69,7 @@ export async function execAsync(bytecode: any[], options?: ExecOptions): Promise
const result = await ASYNC_STL[response.asyncFunctionName](
response.asyncFunctionArgs,
response.asyncFunctionName,
options?.timeout ?? DEFAULT_TIMEOUT
options?.timeout ?? DEFAULT_TIMEOUT_MS
)
vmState.stack.push(result)
} else {
Expand Down Expand Up @@ -105,7 +108,7 @@ export function exec(code: any[] | VMState, options?: ExecOptions): ExecResult {
const declaredFunctions: Record<string, [number, number]> = vmState ? vmState.declaredFunctions : {}
let ip = vmState ? vmState.ip : 1
let ops = vmState ? vmState.ops : 0
const timeout = options?.timeout ?? DEFAULT_TIMEOUT
const timeout = options?.timeout ?? DEFAULT_TIMEOUT_MS
const maxAsyncSteps = options?.maxAsyncSteps ?? DEFAULT_MAX_ASYNC_STEPS

function popStack(): any {
Expand All @@ -122,8 +125,8 @@ export function exec(code: any[] | VMState, options?: ExecOptions): ExecResult {
return bytecode![++ip]
}
function checkTimeout(): void {
if (syncDuration + Date.now() - startTime > timeout * 1000) {
throw new Error(`Execution timed out after ${timeout} seconds. Performed ${ops} ops.`)
if (syncDuration + Date.now() - startTime > timeout) {
throw new Error(`Execution timed out after ${timeout / 1000} seconds. Performed ${ops} ops.`)
}
}

Expand Down
3 changes: 2 additions & 1 deletion posthog/hogql/bytecode.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dataclasses
from datetime import timedelta
from typing import Any, Optional, cast, TYPE_CHECKING
from collections.abc import Callable

Expand Down Expand Up @@ -388,7 +389,7 @@ def execute_hog(
team: Optional["Team"] = None,
globals: Optional[dict[str, Any]] = None,
functions: Optional[dict[str, Callable[..., Any]]] = None,
timeout=10,
timeout=timedelta(seconds=10),
) -> BytecodeResult:
source_code = source_code.strip()
if source_code.count("\n") == 0:
Expand Down

0 comments on commit 8ecec15

Please sign in to comment.