Skip to content

Commit 684fe39

Browse files
authored
Prevent oversized payloads (#280)
* Prevent oversized payloads * upped version
1 parent a04eda0 commit 684fe39

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to the [Nucleus Python Client](https://github.com/scaleapi/n
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.10.1](https://github.com/scaleapi/nucleus-python-client/releases/tag/v0.10.1) - 2022-04-21
9+
10+
### Added
11+
- Added check for payload size
12+
813
## [0.10.0](https://github.com/scaleapi/nucleus-python-client/releases/tag/v0.10.0)) - 2022-04-21
914

1015
### Added

nucleus/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
LABEL_KEY = "label"
8686
LABELS_KEY = "labels"
8787
MASK_URL_KEY = "mask_url"
88+
MAX_PAYLOAD_SIZE = 0x1FFFFFE8 # Set to max string size since we currently convert payloads to strings for processing on the server-side
8889
MESSAGE_KEY = "message"
8990
METADATA_KEY = "metadata"
9091
MODEL_ID_KEY = "model_id"

nucleus/utils.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
KEYPOINTS_TYPE,
3333
LAST_PAGE,
3434
LINE_TYPE,
35+
MAX_PAYLOAD_SIZE,
3536
MULTICATEGORY_TYPE,
3637
PAGE_SIZE,
3738
PAGE_TOKEN,
@@ -247,6 +248,13 @@ def serialize_and_write(
247248
],
248249
file_pointer,
249250
):
251+
"""Helper function serialize and write payload to file
252+
253+
Args:
254+
upload_units: Sequence of items, annotations or scenes
255+
file_pointer: Pointer of the file to write to
256+
"""
257+
bytes_written = 0
250258
if len(upload_units) == 0:
251259
raise ValueError(
252260
"Expecting at least one object when serializing objects to upload, but got zero. Please try again."
@@ -256,9 +264,9 @@ def serialize_and_write(
256264
if isinstance(
257265
unit, (DatasetItem, Annotation, LidarScene, VideoScene)
258266
):
259-
file_pointer.write(unit.to_json() + "\n")
267+
bytes_written += file_pointer.write(unit.to_json() + "\n")
260268
else:
261-
file_pointer.write(json.dumps(unit) + "\n")
269+
bytes_written += file_pointer.write(json.dumps(unit) + "\n")
262270
except TypeError as e:
263271
type_name = type(unit).__name__
264272
message = (
@@ -273,6 +281,10 @@ def serialize_and_write(
273281
)
274282
message += f"The specific error was {e}"
275283
raise ValueError(message) from e
284+
if bytes_written > MAX_PAYLOAD_SIZE:
285+
raise ValueError(
286+
f"Payload of {bytes_written} bytes exceed maximum payload size of {MAX_PAYLOAD_SIZE} bytes. Please reduce payload size and try again."
287+
)
276288

277289

278290
def upload_to_presigned_url(presigned_url: str, file_pointer: IO):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ exclude = '''
2121

2222
[tool.poetry]
2323
name = "scale-nucleus"
24-
version = "0.10.0"
24+
version = "0.10.1"
2525
description = "The official Python client library for Nucleus, the Data Platform for AI"
2626
license = "MIT"
2727
authors = ["Scale AI Nucleus Team <nucleusapi@scaleapi.com>"]

0 commit comments

Comments
 (0)