Skip to content

Truncate action log to avoid json string decoding overflow #8757

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion frontend/javascripts/admin/rest_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,9 +777,10 @@ export function getUpdateActionLog(
annotationId: string,
oldestVersion?: number,
newestVersion?: number,
truncateActionLog: boolean = false,
): Promise<Array<APIUpdateActionBatch>> {
return doWithToken((token) => {
const params = new URLSearchParams();
const params = new URLSearchParams([["truncate", truncateActionLog.toString()]]);
params.set("token", token);
if (oldestVersion != null) {
params.set("oldestVersion", oldestVersion.toString());
Expand Down
1 change: 1 addition & 0 deletions frontend/javascripts/viewer/view/version_list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ async function getUpdateActionLogPage(
annotationId,
oldestVersionInPage,
newestVersionInPage,
true,
);

// The backend won't send the version 0 as that does not exist. The frontend however
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class TSAnnotationService @Inject()(val remoteWebknossosClient: TSRemoteWebknoss
private lazy val materializedAnnotationWithTracingCache =
AlfuCache[ObjectId, AlfuCache[Long, AnnotationWithTracings]](maxCapacity = 1000)

private val maxUpdateActionLogBatchSize = 1000

private def newInnerCache(implicit ec: ExecutionContext): Fox[AlfuCache[Long, AnnotationWithTracings]] =
Fox.successful(AlfuCache[Long, AnnotationWithTracings](maxCapacity = 2))

Expand Down Expand Up @@ -240,7 +242,7 @@ class TSAnnotationService @Inject()(val remoteWebknossosClient: TSRemoteWebknoss
else
tracingDataStore.annotations.put(annotationId.toString, version, annotationProto)

def updateActionLog(annotationId: ObjectId, newestVersion: Long, oldestVersion: Long)(
def updateActionLog(annotationId: ObjectId, newestVersion: Long, oldestVersion: Long, truncate: Boolean)(
implicit ec: ExecutionContext): Fox[JsValue] = {
def versionedTupleToJson(tuple: (Long, List[UpdateAction])): JsObject =
Json.obj(
Expand All @@ -258,7 +260,9 @@ class TSAnnotationService @Inject()(val remoteWebknossosClient: TSRemoteWebknoss
Some(batchTo),
Some(batchFrom))(fromJsonBytes[List[UpdateAction]])
}
} yield Json.toJson(updateActionBatches.flatten.map(versionedTupleToJson))
truncatedUpdateActionBatches = if (truncate) updateActionBatches.map(_.take(maxUpdateActionLogBatchSize))
else updateActionBatches
} yield Json.toJson(truncatedUpdateActionBatches.flatten.map(versionedTupleToJson))
Comment on lines +263 to +265
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider total response size in addition to per-batch truncation.

The current implementation truncates each batch to 1000 items but then flattens all batches together. This could still result in very large JSON responses if there are many batches, potentially not fully addressing the JSON decoding overflow issue.

Consider also limiting the total number of batches or implementing a global response size limit to ensure the final JSON stays within frontend decoding limits.

 truncatedUpdateActionBatches = if (truncate) updateActionBatches.map(_.take(maxUpdateActionLogBatchSize))
 else updateActionBatches
+// Consider also limiting total response size:
+finalBatches = if (truncate) truncatedUpdateActionBatches.take(maxBatchCount) else truncatedUpdateActionBatches
-} yield Json.toJson(truncatedUpdateActionBatches.flatten.map(versionedTupleToJson))
+} yield Json.toJson(finalBatches.flatten.map(versionedTupleToJson))

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In
webknossos-tracingstore/app/com/scalableminds/webknossos/tracingstore/annotation/TSAnnotationService.scala
around lines 263 to 265, the code truncates each batch to a maximum size but
then flattens all batches, which can still produce an excessively large JSON
response. To fix this, implement an additional limit on the total number of
batches or the overall size of the combined response before converting to JSON,
ensuring the final output stays within frontend decoding limits.

}

def findEditableMappingInfo(annotationId: ObjectId, tracingId: String, version: Option[Long] = None)(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,16 @@ class TSAnnotationController @Inject()(

def updateActionLog(annotationId: ObjectId,
newestVersion: Option[Long] = None,
oldestVersion: Option[Long] = None): Action[AnyContent] = Action.async { implicit request =>
oldestVersion: Option[Long] = None,
truncate: Option[Boolean] = None): Action[AnyContent] = Action.async { implicit request =>
log() {
accessTokenService.validateAccessFromTokenContext(UserAccessRequest.readAnnotation(annotationId)) {
for {
newestMaterializableVersion <- annotationService.currentMaterializableVersion(annotationId)
updateLog <- annotationService.updateActionLog(annotationId,
newestVersion.getOrElse(newestMaterializableVersion),
oldestVersion.getOrElse(0))
oldestVersion.getOrElse(0),
truncate.getOrElse(false))
} yield Ok(updateLog)
}
}
Expand Down
2 changes: 1 addition & 1 deletion webknossos-tracingstore/conf/tracingstore.latest.routes
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ GET /health
POST /annotation/save @com.scalableminds.webknossos.tracingstore.controllers.TSAnnotationController.save(annotationId: ObjectId)
GET /annotation/:annotationId @com.scalableminds.webknossos.tracingstore.controllers.TSAnnotationController.get(annotationId: ObjectId, version: Option[Long])
POST /annotation/:annotationId/update @com.scalableminds.webknossos.tracingstore.controllers.TSAnnotationController.update(annotationId: ObjectId)
GET /annotation/:annotationId/updateActionLog @com.scalableminds.webknossos.tracingstore.controllers.TSAnnotationController.updateActionLog(annotationId: ObjectId, newestVersion: Option[Long], oldestVersion: Option[Long])
GET /annotation/:annotationId/updateActionLog @com.scalableminds.webknossos.tracingstore.controllers.TSAnnotationController.updateActionLog(annotationId: ObjectId, newestVersion: Option[Long], oldestVersion: Option[Long], truncate: Option[Boolean])
GET /annotation/:annotationId/newestVersion @com.scalableminds.webknossos.tracingstore.controllers.TSAnnotationController.newestVersion(annotationId: ObjectId)
POST /annotation/:annotationId/duplicate @com.scalableminds.webknossos.tracingstore.controllers.TSAnnotationController.duplicate(annotationId: ObjectId, newAnnotationId: ObjectId, ownerId: ObjectId, requestingUserId: ObjectId, version: Option[Long], isFromTask: Boolean, datasetBoundingBox: Option[String])
POST /annotation/:annotationId/resetToBase @com.scalableminds.webknossos.tracingstore.controllers.TSAnnotationController.resetToBase(annotationId: ObjectId)
Expand Down