A small module builds a preview window for a list of strings and saves it to a temporary file. Right now the behavior looks unstable: on repeated calls, different instances can affect each other, and when the operation is interrupted, the resource sometimes remains open.
PreviewWindow must accept prefix, limit, and source.preview() returns a list of strings in the format "<prefix>:<item>".limit elements.write_preview(path) writes the preview result to a file and always closes the file after completion.write_preview(path) must finish correctly even if CancelledError occurs during writing.PreviewWindow instances with different prefix values.preview() on both instances one after another.source and then call write_preview().preview() must not return more than limit elements.write_preview(), the file is closed regardless of whether writing completed successfully or was canceled.pytest is not required; checks are based on directly running the module and asynchronous scenariosasyncio is used to demonstrate task cancellationUse line comments to explain the problem, its impact, the fix and how to test it.