Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(batch-exports): Remove zero unicode from error message #27195

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions posthog/temporal/batch_exports/batch_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,14 @@ async def finish_batch_export_run(inputs: FinishBatchExportRunInputs) -> None:
for key, value in dataclasses.asdict(inputs).items()
if key not in not_model_params and value is not None
}

latest_error = update_params.get("latest_error", None)
if latest_error is not None and isinstance(latest_error, str):
# NUL (\x00) bytes are not allowed in PostgreSQL, so we replace them in
# the free text field `latest_error`.
latest_error = latest_error.replace("\x00", "")
update_params["latest_error"] = latest_error

batch_export_run = await database_sync_to_async(update_batch_export_run)(
run_id=uuid.UUID(inputs.id),
finished_at=dt.datetime.now(dt.UTC),
Expand Down
35 changes: 35 additions & 0 deletions posthog/temporal/tests/batch_exports/test_run_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,38 @@ async def test_finish_batch_export_run_never_pauses_with_small_check_window(acti
await sync_to_async(batch_export.refresh_from_db)()

assert batch_export.paused is False


@pytest.mark.django_db(transaction=True)
@pytest.mark.asyncio
async def test_finish_batch_export_run_handles_nul_bytes(activity_environment, team, batch_export):
"""Test if 'finish_batch_export_run' will not fail in the prescence of a NUL byte."""
start = dt.datetime(2023, 4, 24, tzinfo=dt.UTC)
end = dt.datetime(2023, 4, 25, tzinfo=dt.UTC)

inputs = StartBatchExportRunInputs(
team_id=team.id,
batch_export_id=str(batch_export.id),
data_interval_start=start.isoformat(),
data_interval_end=end.isoformat(),
)

batch_export_id = str(batch_export.id)

run_id = await activity_environment.run(start_batch_export_run, inputs)

finish_inputs = FinishBatchExportRunInputs(
id=str(run_id),
batch_export_id=batch_export_id,
status=BatchExportRun.Status.FAILED,
team_id=inputs.team_id,
latest_error="Oh No a NUL byte: \x00!",
)

await activity_environment.run(finish_batch_export_run, finish_inputs)

runs = BatchExportRun.objects.filter(id=run_id)
run = await sync_to_async(runs.first)()
assert run is not None
assert run.status == "Failed"
assert run.latest_error == "Oh No a NUL byte: !"
Loading