# Fix freeze when binary content written to open empty file \(\#53074\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/109c2238aa645dd9e8d21e3dc6d2597e61146eda)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 109c2238aa645dd9e8d21e3dc6d2597e61146eda

Requested commit: 109c2238aa645dd9e8d21e3dc6d2597e61146eda

Commit: 109c2238aa645dd9e8d21e3dc6d2597e61146eda

Tree: ea23ea7436f8cebc8212f7374e39ca3639d6ce8b

Author: deadcode-walker

Committer: GitHub

## Message

```
Fix freeze when binary content written to open empty file (#53074)

Opening an empty file then writing binary content to it on disk causes a
permanent freeze (not a crash — requires force kill).

`reload_impl` loads raw bytes via `load_bytes` and decodes with
`encoding_rs` but never checks if the content is binary. The existing
binary check in `decode_file_text` only runs on fresh opens, not
reloads. So binary content enters the buffer as lossy UTF-8.

Binary data has almost no newlines, producing a single enormous row. The
wrap map's sync fast path in `flush_edits` checks row count (< 100 rows)
but not line length, so it runs `wrap_line` on a multi-MB single line
synchronously on the main thread via `smol::block_on`. Font shaping
millions of non-ASCII replacement characters blocks the UI thread
indefinitely.

Two fixes, both one-condition guards:
- Null-byte check in `reload_impl` before decoding, same heuristic
(first 8000 bytes) used by `decode_file_text` on fresh opens. Binary
content never enters the buffer.
- Column-length guard (`MAX_SYNC_WRAP_COLUMNS = 10_000`) on the wrap map
sync fast path so absurdly long lines fall through to the async
background path. Defense in depth for any single-line content that's too
long to shape synchronously.

## Test plan
- [ ] Open an empty file in Zed, write binary content to it externally
(e.g. `cp /bin/ls /path/to/open-file`) — should show "Binary files are
not supported" instead of freezing
- [ ] Open a compressed file (zip, gz) that starts empty and gets filled
— same behavior
- [ ] Normal text file reload still works (no regression)
- [ ] Very long single-line text files (>10k columns) don't freeze the
editor

---------

Co-authored-by: deadcode-walker <268043493+deadcode-walker@users.noreply.github.com>
Co-authored-by: Smit Barmase <heysmitbarmase@gmail.com>
```

## Parents

- [62bd61a6793c5f983fbc1700825208f19d780d52](https://git.cafe/gitcafe/zed/commit/62bd61a6793c5f983fbc1700825208f19d780d52?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/109c2238aa645dd9e8d21e3dc6d2597e61146eda?format=markdown)
