DrKLO/Telegram · error

%s: can't write to stdout

Error message

%s: can't write to stdout

What it means

Same write-error condition as error 151, but fired when output goes to stdout (file_index >= argc). JFWRITE returns fewer than size bytes with ferror(fp) true on stdout. This happens when the downstream consumer of stdout closes the pipe before jpegtran finishes writing.

Source

Thrown at TMessagesProj/jni/mozjpeg/jpegtran.c:673

  if (jpeg_c_int_param_supported(&dstinfo, JINT_COMPRESS_PROFILE) &&
      jpeg_c_get_int_param(&dstinfo, JINT_COMPRESS_PROFILE)
        == JCP_MAX_COMPRESSION) {
    size_t nbytes;
    
    unsigned char *buffer = outbuffer;
    unsigned long size = outsize;
    if (prefer_smallest && insize < size) {
      size = insize;
      buffer = inbuffer;
    }

    nbytes = JFWRITE(fp, buffer, size);
    if (nbytes < size && ferror(fp)) {
      if (file_index < argc)
        fprintf(stderr, "%s: can't write to %s\n", progname,
                argv[file_index]);
      else
        fprintf(stderr, "%s: can't write to stdout\n", progname);
    }
  }
#endif
    
  jpeg_destroy_compress(&dstinfo);
  (void)jpeg_finish_decompress(&srcinfo);
  jpeg_destroy_decompress(&srcinfo);

  /* Close output file, if we opened it */
  if (fp != stdout)
    fclose(fp);

#ifdef PROGRESS_REPORT
  end_progress_monitor((j_common_ptr)&dstinfo);
#endif

  free(inbuffer);
  free(outbuffer);

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Write to a file instead of stdout to avoid broken-pipe issues
  2. Use `set -o pipefail` in bash to detect pipeline failures
  3. If piping to a viewer, ensure the viewer reads the entire stream

Example fix

// before
jpegtran input.jpg | head -c 1000 > preview

// after
jpegtran input.jpg -outfile /tmp/out.jpg
head -c 1000 /tmp/out.jpg > preview
Defensive patterns

Strategy: validation

Validate before calling

// Avoid piping to stdout; write to a file instead
// In a shell script:
// before: jpegtran input.jpg | viewer
// after:
TMP=$(mktemp)
jpegtran input.jpg -outfile "$TMP"
if [ $? -eq 0 ]; then
    cat "$TMP" | viewer
fi
rm -f "$TMP"

Try / catch

// If you must pipe, use pipefail and check
set -o pipefail
jpegtran input.jpg | downstream
echo "pipeline exit: $?"

Prevention

When it happens

Trigger: Piping jpegtran output to `head` which closes the pipe after reading enough. Writing to a redirected file whose disk fills up. A downstream process crashes or is killed. A network socket (via netcat) drops mid-transfer.

Common situations: Shell pipeline `jpegtran ... | viewer` where viewer exits early. Redirecting to a file on full storage. Using process substitution that aborts.

Related errors


AI-assisted analysis of DrKLO/Telegram@45ab8f4308 (2026-08-14). Data as JSON: /api/errors/5e899547bb64e71e. Report an issue: GitHub.