Skip to content

[Bug]: output file not cleared before writing, causing broken JSON #8231

@rite7sh

Description

@rite7sh

What happened?

When I run uiconv multiple times on the same output file, the file is not cleared before writing. If the new output is smaller than the previous one, some old data remains at the end of the file. This results in broken/invalid JSON.
the test i used:

package main

import (
	"fmt"
	"os"
)

func main() {
	file := "/tmp/out.json"

	long := `{"data": [{"traceID": "abc123", "spans": [{"spanID": "s1", "operationName": "op1"}]}]}`
	short := `{"data": []}`

	// first write
	f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		panic(err)
	}
	f.WriteString(long)
	f.Close()

	data1, _ := os.ReadFile(file)
	fmt.Println("first write:")
	fmt.Println(string(data1))
	fmt.Println("size:", len(data1))
	fmt.Println()

	// second write (same flags)
	f, err = os.OpenFile(file, os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		panic(err)
	}
	f.WriteString(short)
	f.Close()

	data2, _ := os.ReadFile(file)
	fmt.Println("second write:")
	fmt.Println(string(data2))
	fmt.Println("size:", len(data2))
	fmt.Println()

	// fix with truncation
	f, err = os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
	if err != nil {
		panic(err)
	}
	f.WriteString(short)
	f.Close()

	data3, _ := os.ReadFile(file)
	fmt.Println("with trunc:")
	fmt.Println(string(data3))
	fmt.Println("size:", len(data3))

	os.Remove(file)
}

Steps to reproduce

  1. Run uiconv with a large input file and save output to out.json
  2. Run uiconv again with a smaller input file using the same output file
  3. Open out.json

I noticed extra old data at the end of the file.

Expected behavior

The output file should be cleared before writing.

It should only contain the latest JSON and no leftover data from previous runs.

Relevant log output

second write:
{"data": []}traceID": "abc123", "spans": [{"spanID": "s1", "operationName": "op1"}]}]}
size: 86

Screenshot

did a 3 write test and the log output :
Image

Additional context

In extractor.go:

Image

The file is opened using: os.OpenFile(uiFile, os.O_CREATE|os.O_WRONLY, os.ModePerm),

Since O_TRUNC is not used, the file is not cleared before writing.
A simple fix would be:
os.OpenFile(uiFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)

Jaeger backend version

main branch (local build)

SDK

irrelevant to bug

Pipeline

NA

Stogage backend

NA

Operating system

Linux (WSL)

Deployment model

Local development (running code from repository)

Deployment configs

NA

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions