diff --git a/src/borg/repository.py b/src/borg/repository.py
index ca55c698..dada3c14 100644
--- a/src/borg/repository.py
+++ b/src/borg/repository.py
@@ -1176,6 +1176,8 @@ def cleanup(self, transaction_id):
         count = 0
         for segment, filename in self.segment_iterator(reverse=True):
             if segment > transaction_id:
+                if segment in self.fds:
+                    del self.fds[segment]
                 truncate_and_unlink(filename)
                 count += 1
             else:
@@ -1232,6 +1234,12 @@ def get_write_fd(self, no_new=False, raise_full=False):
             self._write_fd = SyncFile(self.segment_filename(self.segment), binary=True)
             self._write_fd.write(MAGIC)
             self.offset = MAGIC_LEN
+            if self.segment in self.fds:
+                # we may have a cached fd for a segment file we already deleted and
+                # we are writing now a new segment file to same file name. get rid of
+                # of the cached fd that still refers to the old file, so it will later
+                # get repopulated (on demand) with a fd that refers to the new file.
+                del self.fds[self.segment]
         return self._write_fd
 
     def get_fd(self, segment):

with-lock crashes with 1.1.0-rc2
I just upgraded from borgbackup-1.1.0-b3 to rc2, found an issue within minutes.

A simple example, borgbackup is version 1.1.0-rc2
> $ borgbackup --version
borgbackup 1.1.0rc2
> $ borgbackup with-lock mail-01.borg ls
[Files are listed here]
> Data integrity error: Segment entry data short read [segment 391, offset 1590]: expected 1890652, got 2497 bytes
> Traceback (most recent call last):
>   File "borg/archiver.py", line 3968, in main
>   File "borg/archiver.py", line 3896, in run
>   File "borg/archiver.py", line 148, in wrapper
>   File "borg/archiver.py", line 1677, in do_with_lock
>   File "borg/repository.py", line 411, in commit
>   File "borg/repository.py", line 685, in compact_segments
>   File "borg/repository.py", line 1292, in iter_objects
>   File "borg/repository.py", line 1373, in _read
> borg.helpers.IntegrityError: Data integrity error: Segment entry data short read [segment 391, offset 1590]: expected 1890652, got 2497 bytes
> 
> Platform: Linux thor 4.8.0-1-amd64 #1 SMP Debian 4.8.5-1 (2016-10-28) x86_64 
> Linux: debian stretch/sid 
> Borg: 1.1.0rc2  Python: CPython 3.5.3
> PID: 31905  CWD: /data/borg-WedFriSun
> sys.argv: ['borgbackup', 'with-lock', 'mail-01.borg', 'ls']
> SSH_ORIGINAL_COMMAND: None
> 

With 1.1.0-b3 it works fine.

No errors when running check
> $ borgbackup check mail-01.borg
> Enter passphrase for key /data/borg-WedFriSun/mail-01.borg: 
>

diff --git a/src/borg/testsuite/repository.py b/src/borg/testsuite/repository.py
index e29c7d53..1bec1400 100644
--- a/src/borg/testsuite/repository.py
+++ b/src/borg/testsuite/repository.py
@@ -210,6 +210,23 @@ def test_sparse_delete(self):
         self.repository.commit()
         assert 0 not in [segment for segment, _ in self.repository.io.segment_iterator()]
 
+    def test_uncommitted_garbage(self):
+        # uncommitted garbage should be no problem, it is cleaned up automatically.
+        # we just have to be careful with invalidation of cached FDs in LoggedIO.
+        self.repository.put(H(0), b'foo')
+        self.repository.commit()
+        # write some crap to a uncommitted segment file
+        last_segment = self.repository.io.get_latest_segment()
+        with open(self.repository.io.segment_filename(last_segment + 1), 'wb') as f:
+            f.write(MAGIC + b'crapcrapcrap')
+        self.repository.close()
+        # usually, opening the repo and starting a transaction should trigger a cleanup.
+        self.repository = self.open()
+        with self.repository:
+            self.repository.put(H(0), b'bar')  # this may trigger compact_segments()
+            self.repository.commit()
+        # the point here is that nothing blows up with an exception.
+
 
 class RepositoryCommitTestCase(RepositoryTestCaseBase):
 

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest pytest-xdist pytest-cov
apt-get update apt-get install -y gcc libacl1-dev libacl1 libssl-dev liblz4-dev libzstd-dev build-essential pkg-config python3-pkgconfig
3.9
requirements.d/development.txt requirements.d/docs.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
alabaster==0.7.16
babel==2.17.0
-e git+https://github.com/borgbackup/borg.git@9afec263c98c0cb6c733c5c451d26f1cb0a251fd#egg=borgbackup
cachetools==5.5.2
certifi==2025.1.31
chardet==5.2.0
charset-normalizer==3.4.1
colorama==0.4.6
coverage==7.8.0
Cython==3.0.12
distlib==0.3.9
docutils==0.21.2
exceptiongroup==1.2.2
execnet==2.1.1
filelock==3.18.0
guzzle_sphinx_theme==0.7.11
idna==3.10
imagesize==1.4.1
importlib_metadata==8.6.1
iniconfig==2.1.0
Jinja2==3.1.6
MarkupSafe==3.0.2
msgpack-python==0.5.6
packaging==24.2
platformdirs==4.3.7
pluggy==1.5.0
py-cpuinfo==9.0.0
Pygments==2.19.1
pyproject-api==1.9.0
pytest==8.3.5
pytest-benchmark==5.1.0
pytest-cov==6.0.0
pytest-xdist==3.6.1
pyzmq==26.3.0
requests==2.32.3
setuptools-scm==8.2.0
snowballstemmer==2.2.0
Sphinx==7.4.7
sphinxcontrib-applehelp==2.0.0
sphinxcontrib-devhelp==2.0.0
sphinxcontrib-htmlhelp==2.1.0
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==2.0.0
sphinxcontrib-serializinghtml==2.0.0
tomli==2.2.1
tox==4.25.0
typing_extensions==4.13.0
urllib3==2.3.0
virtualenv==20.29.3
zipp==3.21.0

name: borg
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - alabaster==0.7.16
      - babel==2.17.0
      - cachetools==5.5.2
      - certifi==2025.1.31
      - chardet==5.2.0
      - charset-normalizer==3.4.1
      - colorama==0.4.6
      - coverage==7.8.0
      - cython==3.0.12
      - distlib==0.3.9
      - docutils==0.21.2
      - exceptiongroup==1.2.2
      - execnet==2.1.1
      - filelock==3.18.0
      - guzzle-sphinx-theme==0.7.11
      - idna==3.10
      - imagesize==1.4.1
      - importlib-metadata==8.6.1
      - iniconfig==2.1.0
      - jinja2==3.1.6
      - markupsafe==3.0.2
      - msgpack-python==0.5.6
      - packaging==24.2
      - platformdirs==4.3.7
      - pluggy==1.5.0
      - py-cpuinfo==9.0.0
      - pygments==2.19.1
      - pyproject-api==1.9.0
      - pytest==8.3.5
      - pytest-benchmark==5.1.0
      - pytest-cov==6.0.0
      - pytest-xdist==3.6.1
      - pyzmq==26.3.0
      - requests==2.32.3
      - setuptools-scm==8.2.0
      - snowballstemmer==2.2.0
      - sphinx==7.4.7
      - sphinxcontrib-applehelp==2.0.0
      - sphinxcontrib-devhelp==2.0.0
      - sphinxcontrib-htmlhelp==2.1.0
      - sphinxcontrib-jsmath==1.0.1
      - sphinxcontrib-qthelp==2.0.0
      - sphinxcontrib-serializinghtml==2.0.0
      - tomli==2.2.1
      - tox==4.25.0
      - typing-extensions==4.13.0
      - urllib3==2.3.0
      - virtualenv==20.29.3
      - zipp==3.21.0
prefix: /opt/conda/envs/borg

src/borg/testsuite/repository.py::LocalRepositoryTestCase::test_uncommitted_garbage
src/borg/testsuite/repository.py::RemoteRepositoryTestCase::test1 src/borg/testsuite/repository.py::RemoteRepositoryTestCase::test_consistency src/borg/testsuite/repository.py::RemoteRepositoryTestCase::test_invalid_rpc src/borg/testsuite/repository.py::RemoteRepositoryTestCase::test_max_data_size src/borg/testsuite/repository.py::RemoteRepositoryTestCase::test_rpc_exception_transport src/borg/testsuite/repository.py::RemoteRepositoryCheckTestCase::test_repair_corrupted_commit_segment src/borg/testsuite/repository.py::RemoteRepositoryCheckTestCase::test_repair_corrupted_segment src/borg/testsuite/repository.py::RemoteRepositoryCheckTestCase::test_repair_missing_commit_segment src/borg/testsuite/repository.py::RemoteRepositoryCheckTestCase::test_repair_no_commits
src/borg/testsuite/repository.py::RepositoryTestCase::test1 src/borg/testsuite/repository.py::RepositoryTestCase::test2 src/borg/testsuite/repository.py::RepositoryTestCase::test_consistency src/borg/testsuite/repository.py::RepositoryTestCase::test_consistency2 src/borg/testsuite/repository.py::RepositoryTestCase::test_list src/borg/testsuite/repository.py::RepositoryTestCase::test_max_data_size src/borg/testsuite/repository.py::RepositoryTestCase::test_overwrite_in_same_transaction src/borg/testsuite/repository.py::RepositoryTestCase::test_scan src/borg/testsuite/repository.py::RepositoryTestCase::test_single_kind_transactions src/borg/testsuite/repository.py::LocalRepositoryTestCase::test_sparse1 src/borg/testsuite/repository.py::LocalRepositoryTestCase::test_sparse2 src/borg/testsuite/repository.py::LocalRepositoryTestCase::test_sparse_delete src/borg/testsuite/repository.py::RepositoryCommitTestCase::test_crash_before_compact_segments src/borg/testsuite/repository.py::RepositoryCommitTestCase::test_crash_before_deleting_compacted_segments src/borg/testsuite/repository.py::RepositoryCommitTestCase::test_crash_before_write_index src/borg/testsuite/repository.py::RepositoryCommitTestCase::test_ignores_commit_tag_in_data src/borg/testsuite/repository.py::RepositoryCommitTestCase::test_moved_deletes_are_tracked src/borg/testsuite/repository.py::RepositoryCommitTestCase::test_replay_lock_upgrade src/borg/testsuite/repository.py::RepositoryCommitTestCase::test_replay_lock_upgrade_old src/borg/testsuite/repository.py::RepositoryCommitTestCase::test_replay_of_missing_index src/borg/testsuite/repository.py::RepositoryCommitTestCase::test_shadow_index_rollback src/borg/testsuite/repository.py::RepositoryCommitTestCase::test_shadowed_entries_are_preserved src/borg/testsuite/repository.py::RepositoryAppendOnlyTestCase::test_append_only src/borg/testsuite/repository.py::RepositoryAppendOnlyTestCase::test_destroy_append_only src/borg/testsuite/repository.py::RepositoryFreeSpaceTestCase::test_additional_free_space src/borg/testsuite/repository.py::RepositoryFreeSpaceTestCase::test_create_free_space src/borg/testsuite/repository.py::QuotaTestCase::test_exceed_quota src/borg/testsuite/repository.py::QuotaTestCase::test_tracking src/borg/testsuite/repository.py::NonceReservation::test_commit_nonce_reservation src/borg/testsuite/repository.py::NonceReservation::test_commit_nonce_reservation_asserts src/borg/testsuite/repository.py::NonceReservation::test_get_free_nonce src/borg/testsuite/repository.py::NonceReservation::test_get_free_nonce_asserts src/borg/testsuite/repository.py::RepositoryAuxiliaryCorruptionTestCase::test_corrupted_hints src/borg/testsuite/repository.py::RepositoryAuxiliaryCorruptionTestCase::test_deleted_hints src/borg/testsuite/repository.py::RepositoryAuxiliaryCorruptionTestCase::test_deleted_index src/borg/testsuite/repository.py::RepositoryAuxiliaryCorruptionTestCase::test_index src/borg/testsuite/repository.py::RepositoryAuxiliaryCorruptionTestCase::test_index_corrupted src/borg/testsuite/repository.py::RepositoryAuxiliaryCorruptionTestCase::test_index_corrupted_without_integrity src/borg/testsuite/repository.py::RepositoryAuxiliaryCorruptionTestCase::test_index_outside_transaction src/borg/testsuite/repository.py::RepositoryAuxiliaryCorruptionTestCase::test_subtly_corrupted_hints src/borg/testsuite/repository.py::RepositoryAuxiliaryCorruptionTestCase::test_subtly_corrupted_hints_without_integrity src/borg/testsuite/repository.py::RepositoryAuxiliaryCorruptionTestCase::test_unknown_integrity_version src/borg/testsuite/repository.py::RepositoryAuxiliaryCorruptionTestCase::test_unreadable_hints src/borg/testsuite/repository.py::RepositoryAuxiliaryCorruptionTestCase::test_unreadable_index src/borg/testsuite/repository.py::RepositoryCheckTestCase::test_crash_before_compact src/borg/testsuite/repository.py::RepositoryCheckTestCase::test_repair_corrupted_commit_segment src/borg/testsuite/repository.py::RepositoryCheckTestCase::test_repair_corrupted_segment src/borg/testsuite/repository.py::RepositoryCheckTestCase::test_repair_index_too_new src/borg/testsuite/repository.py::RepositoryCheckTestCase::test_repair_missing_commit_segment src/borg/testsuite/repository.py::RepositoryCheckTestCase::test_repair_missing_index src/borg/testsuite/repository.py::RepositoryCheckTestCase::test_repair_missing_segment src/borg/testsuite/repository.py::RepositoryCheckTestCase::test_repair_no_commits src/borg/testsuite/repository.py::RemoteRepositoryTestCase::test2 src/borg/testsuite/repository.py::RemoteRepositoryTestCase::test_borg_cmd src/borg/testsuite/repository.py::RemoteRepositoryTestCase::test_consistency2 src/borg/testsuite/repository.py::RemoteRepositoryTestCase::test_list src/borg/testsuite/repository.py::RemoteRepositoryTestCase::test_overwrite_in_same_transaction src/borg/testsuite/repository.py::RemoteRepositoryTestCase::test_scan src/borg/testsuite/repository.py::RemoteRepositoryTestCase::test_single_kind_transactions src/borg/testsuite/repository.py::RemoteRepositoryTestCase::test_ssh_cmd src/borg/testsuite/repository.py::RemoteLegacyFree::test_legacy_free src/borg/testsuite/repository.py::RemoteRepositoryCheckTestCase::test_crash_before_compact src/borg/testsuite/repository.py::RemoteRepositoryCheckTestCase::test_repair_index_too_new src/borg/testsuite/repository.py::RemoteRepositoryCheckTestCase::test_repair_missing_index src/borg/testsuite/repository.py::RemoteRepositoryCheckTestCase::test_repair_missing_segment src/borg/testsuite/repository.py::RemoteLoggerTestCase::test_info_to_correct_local_child src/borg/testsuite/repository.py::RemoteLoggerTestCase::test_post11_format_messages src/borg/testsuite/repository.py::RemoteLoggerTestCase::test_pre11_format_messages src/borg/testsuite/repository.py::RemoteLoggerTestCase::test_remote_messages_screened src/borg/testsuite/repository.py::RemoteLoggerTestCase::test_stderr_messages
src/borg/repository.py
src/borg/repository.py