hashicorp/nomad · error

failed to write string to eventstream.json; chars written: %

Error message

failed to write string to eventstream.json; chars written: %d, Err: %w

What it means

After writing each event's JSON bytes, captureEventStream writes a trailing "\n" via fh.WriteString to keep eventstream.json line-delimited. If that small write fails, the error is recorded in the multierror with the number of chars written. Like the bytes-write error, it signals the capture file became unwritable mid-run.

Source

Thrown at command/operator_debug.go:998

				eventCount++
				c.verboseOutf("Event: %4d, Index: %d, Topic: %-10s, Type: %s, FilterKeys: %s", eventCount, e.Index, e.Topic, e.Type, e.FilterKeys)

				bytes, err := json.Marshal(e)
				if err != nil {
					errCount++
					mErrs = multierror.Append(mErrs, fmt.Errorf("failed to marshal json from Topic: %s, Type: %s, Err: %w", e.Topic, e.Type, err))
				}

				n, err := fh.Write(bytes)
				if err != nil {
					errCount++
					mErrs = multierror.Append(mErrs, fmt.Errorf("failed to write bytes to eventstream.json; bytes written: %d, Err: %w", n, err))
					break
				}
				n, err = fh.WriteString("\n")
				if err != nil {
					errCount++
					mErrs = multierror.Append(mErrs, fmt.Errorf("failed to write string to eventstream.json; chars written: %d, Err: %w", n, err))
				}
			}
		case <-c.ctx.Done():
			c.verboseOutf("Event stream captured %d events, %d frames, %d heartbeats, %d errors", eventCount, channelEventCount, heartbeatCount, errCount)
			return mErrs.ErrorOrNil()
		}
	}
}

// collectAgentHosts calls collectAgentHost for each selected node
func (c *OperatorDebugCommand) collectAgentHosts(client *api.Client) {
	for _, n := range c.nodeIDs {
		c.collectAgentHost(clientDir, n, client)
	}

	for _, n := range c.serverIDs {
		c.collectAgentHost(serverDir, n, client)
	}

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Free disk space — a failure here commonly means the volume filled within the previous write
  2. Check n in the message (usually 0) and whether the preceding event bytes wrote fully
  3. Ensure nothing concurrently deletes or rotates eventstream.json during capture
  4. Re-run the capture to produce a complete, consistently newline-delimited file
Defensive patterns

Strategy: try-catch

Validate before calling

if st, err := fh.Stat(); err != nil {
	return fmt.Errorf("capture file handle unusable: %w", err)
}

Try / catch

n, err := fh.WriteString("\n")
if err != nil {
	errs = multierror.Append(errs, fmt.Errorf("failed to write string to eventstream.json; chars written: %d, Err: %w", n, err))
}

Prevention

When it happens

Trigger: fh.WriteString("\n") returns an error after a successful bytes write — disk just filled, file closed/unlinked, I/O error, or fd exhaustion surfaced on the next write.

Common situations: Disk hitting 0 bytes between two writes of the same event; concurrent rotation/cleanup of the capture dir; NFS or flaky volume dropping the connection mid-capture.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/a31b9c2938a5e2f2. Report an issue: GitHub.