Skip to content

Commit 08ed824

Browse files
committed
update code
1 parent f2b3c6f commit 08ed824

File tree

6 files changed

+88
-62
lines changed

6 files changed

+88
-62
lines changed

.codacy.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ exclude_paths:
88
- "examples/**"
99
- "README.md"
1010
- "cli/README.md"
11+
# Build scripts use subprocess with controlled build-time inputs only
12+
- "python/functionstream-runtime/build.py"
13+
- "python/functionstream-api/build_package.py"
1114

1215
# Exclude some code quality checks (during refactoring phase)
1316
exclude_patterns:

Dockerfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
FROM rust:1-bookworm AS builder
22

33
RUN apt-get update && apt-get install -y --no-install-recommends \
4-
cmake \
4+
cmake=3.25.1-1 \
55
make \
6-
clang \
7-
libclang-dev \
6+
clang=1:14.0-55.7~deb12u1 \
7+
libclang-dev=1:14.0-55.7~deb12u1 \
88
python3 \
99
python3-venv \
1010
python3-pip \
@@ -40,8 +40,8 @@ RUN make build-full
4040
FROM debian:bookworm-slim
4141

4242
RUN apt-get update && apt-get install -y --no-install-recommends \
43-
ca-certificates \
44-
libssl3 \
43+
ca-certificates=20230311+deb12u1 \
44+
libssl3=3.0.18-1~deb12u2 \
4545
&& rm -rf /var/lib/apt/lists/*
4646

4747
WORKDIR /app

python/functionstream-client/src/fs_client/client.py

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,47 @@ def _is_under_site_or_stdlib(origin: Path) -> bool:
9090
return True
9191

9292

93+
def _get_module_origin(
94+
module_name: str,
95+
result: Dict[str, Path],
96+
) -> Optional[Path]:
97+
"""Resolve module origin path from spec or existing result."""
98+
if module_name == "__main__" and module_name in result:
99+
return result[module_name]
100+
try:
101+
spec = importlib.util.find_spec(module_name)
102+
except (ImportError, ValueError, ModuleNotFoundError):
103+
return None
104+
if spec is None or spec.origin is None or spec.origin == "built-in":
105+
return None
106+
return Path(spec.origin)
107+
108+
109+
def _process_module_deps(
110+
module_name: str,
111+
origin: Path,
112+
package: str,
113+
dep_graph: Dict[str, Set[str]],
114+
seen: Set[str],
115+
queue: List[str],
116+
) -> None:
117+
"""Parse module and add its imports to dep_graph and queue."""
118+
try:
119+
tree = ast.parse(origin.read_text(encoding="utf-8"))
120+
except (OSError, SyntaxError):
121+
return
122+
for module_part, level in _get_imported_names(tree):
123+
abs_name = (
124+
module_part
125+
if level == 0
126+
else _resolve_relative(module_part, level, package)
127+
)
128+
if abs_name:
129+
dep_graph[module_name].add(abs_name)
130+
if abs_name not in seen:
131+
queue.append(abs_name)
132+
133+
93134
def _collect_local_deps(
94135
driver_class: Type,
95136
driver_file: Path,
@@ -100,6 +141,7 @@ def _collect_local_deps(
100141
dep_graph: Dict[str, Set[str]] = {}
101142
queue: List[str] = [driver_module_name]
102143
seen: Set[str] = set()
144+
103145
if driver_module_name == "__main__":
104146
result[driver_module_name] = driver_file.resolve()
105147
dep_graph[driver_module_name] = set()
@@ -110,42 +152,22 @@ def _collect_local_deps(
110152
continue
111153
seen.add(module_name)
112154
package = module_name.rpartition(".")[0]
113-
if module_name == "__main__" and module_name in result:
114-
origin = result[module_name]
115-
else:
116-
try:
117-
spec = importlib.util.find_spec(module_name)
118-
except (ImportError, ValueError, ModuleNotFoundError):
119-
continue
120-
if (
121-
spec is None
122-
or spec.origin is None
123-
or spec.origin == "built-in"
124-
):
125-
continue
126-
origin = Path(spec.origin)
155+
156+
origin = _get_module_origin(module_name, result)
157+
if origin is None:
158+
continue
127159
if _is_under_site_or_stdlib(origin):
128160
continue
129161
try:
130162
origin.resolve().relative_to(driver_root)
131163
except ValueError:
132164
continue
165+
133166
result[module_name] = origin
134167
dep_graph[module_name] = set()
135-
try:
136-
tree = ast.parse(origin.read_text(encoding="utf-8"))
137-
except (OSError, SyntaxError):
138-
pass
139-
else:
140-
for module_part, level in _get_imported_names(tree):
141-
if level == 0:
142-
abs_name = module_part
143-
else:
144-
abs_name = _resolve_relative(module_part, level, package)
145-
if abs_name:
146-
dep_graph[module_name].add(abs_name)
147-
if abs_name not in seen:
148-
queue.append(abs_name)
168+
_process_module_deps(
169+
module_name, origin, package, dep_graph, seen, queue
170+
)
149171

150172
return result, dep_graph
151173

python/functionstream-client/src/fs_client/exceptions.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,14 @@ class FunctionStreamTimeoutError(ServerError):
7171

7272
class BadRequestError(ServerError):
7373
"""Raised when the arguments are invalid (INVALID_ARGUMENT)."""
74-
pass
7574

7675

7776
class AuthenticationError(ServerError):
7877
"""Raised when the client is not authenticated (UNAUTHENTICATED)."""
79-
pass
8078

8179

8280
class PermissionDeniedError(ServerError):
8381
"""Raised when the client does not have permission (PERMISSION_DENIED)."""
84-
pass
8582

8683

8784
class NotFoundError(ServerError):

python/functionstream-runtime/src/fs_runtime/runner.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
# limitations under the License.
1212

1313
import importlib.util
14+
import logging
1415
import sys
1516
from typing import List, Optional, Tuple
1617

1718
from fs_api.driver import FSProcessorDriver
1819

20+
logger = logging.getLogger(__name__)
21+
1922
from .store.fs_context import WitContext, convert_config_to_dict
2023

2124

@@ -46,11 +49,13 @@ def fs_exec(class_name: str, modules: List[Tuple[str, bytes]]) -> None:
4649
# Create the module
4750
module = importlib.util.module_from_spec(spec)
4851

49-
# Execute the module source code
50-
# Note: exec is required here for dynamic module loading
51-
# This is a controlled execution environment for user-provided code
52+
# Execute the module source code.
53+
# exec is required: importlib.util.module_from_spec does not execute
54+
# code; Python's import system uses exec internally. This runtime
55+
# is designed to execute trusted user-provided processor code in
56+
# an isolated WASM sandbox. Only deploy code from trusted sources.
5257
code = compile(module_source, f"<{module_name}>", "exec")
53-
exec(code, module.__dict__) # noqa: S102
58+
exec(code, module.__dict__) # noqa: S102 # nosec B102
5459

5560
# Add the module to sys.modules
5661
sys.modules[module_name] = module
@@ -105,47 +110,44 @@ def fs_init(self, config: List[Tuple[str, str]]) -> None:
105110
if _DRIVER:
106111
try:
107112
_DRIVER.init(_CONTEXT, _CONTEXT._CONFIG)
108-
except Exception:
109-
# Silently ignore initialization errors to allow graceful degradation
110-
pass # noqa: S110
113+
except Exception as e:
114+
logger.debug("Driver init failed (graceful degradation): %s", e)
111115

112116
def fs_process(self, source_id: int, data: bytes) -> None:
113117
if not _DRIVER or not _CONTEXT:
114118
return
115119

116120
try:
117121
_DRIVER.process(_CONTEXT, source_id, data)
118-
except Exception:
119-
# Silently ignore processing errors to allow graceful degradation
120-
pass # noqa: S110
122+
except Exception as e:
123+
logger.debug("Process error (graceful degradation): %s", e)
121124

122125
def fs_process_watermark(self, source_id: int, watermark: int) -> None:
123126
if not _DRIVER or not _CONTEXT:
124127
return
125128

126129
try:
127130
_DRIVER.process_watermark(_CONTEXT, source_id, watermark)
128-
except Exception:
129-
# Silently ignore watermark processing errors to allow graceful degradation
130-
pass # noqa: S110
131+
except Exception as e:
132+
logger.debug("Watermark process error (graceful degradation): %s", e)
131133

132134
def fs_take_checkpoint(self, checkpoint_id: int) -> None:
133135
if not _DRIVER or not _CONTEXT:
134136
return
135137

136138
try:
137139
_DRIVER.take_checkpoint(_CONTEXT, checkpoint_id)
138-
except Exception:
139-
# Silently ignore checkpoint errors to allow graceful degradation
140-
pass # noqa: S110
140+
except Exception as e:
141+
logger.debug("Checkpoint error (graceful degradation): %s", e)
141142

142143
def fs_check_heartbeat(self) -> bool:
143144
if not _DRIVER or not _CONTEXT:
144145
return False
145146

146147
try:
147148
return _DRIVER.check_heartbeat(_CONTEXT)
148-
except Exception:
149+
except Exception as e:
150+
logger.debug("Heartbeat check failed (graceful degradation): %s", e)
149151
return False
150152

151153
def fs_close(self) -> None:
@@ -154,9 +156,8 @@ def fs_close(self) -> None:
154156
if _DRIVER and _CONTEXT:
155157
try:
156158
_DRIVER.close(_CONTEXT)
157-
except Exception:
158-
# Silently ignore close errors to ensure cleanup completes
159-
pass # noqa: S110
159+
except Exception as e:
160+
logger.debug("Driver close error (cleanup continues): %s", e)
160161

161162
_DRIVER = None
162163
_CONTEXT = None

python/functionstream-runtime/src/fs_runtime/store/fs_collector.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13+
import logging
1314
from typing import TYPE_CHECKING
1415

16+
logger = logging.getLogger(__name__)
17+
1518
if TYPE_CHECKING:
1619
from wit_world.imports.collector import (
1720
emit as wit_emit,
@@ -34,9 +37,8 @@ def emit(data: bytes, channel: int = 0) -> None:
3437

3538
try:
3639
wit_emit(channel, data)
37-
except Exception:
38-
# Silently ignore emit errors to allow graceful degradation
39-
pass # noqa: S110
40+
except Exception as e:
41+
logger.debug("Emit error (graceful degradation): %s", e)
4042

4143

4244
def emit_watermark(watermark: int, channel: int = 0) -> None:
@@ -45,9 +47,10 @@ def emit_watermark(watermark: int, channel: int = 0) -> None:
4547

4648
try:
4749
wit_emit_watermark(channel, watermark)
48-
except Exception:
49-
# Silently ignore watermark emit errors to allow graceful degradation
50-
pass # noqa: S110
50+
except Exception as e:
51+
logger.debug(
52+
"Watermark emit error (graceful degradation): %s", e
53+
)
5154

5255

5356
__all__ = ['emit', 'emit_watermark']

0 commit comments

Comments
 (0)