· 4 min read
What happens when you cancel a photo import?
One open file, a tray of half-made thumbnails, and two names for the same pixels. Three things programming languages can now help us keep straight.

You pick the wrong folder. The photo app starts making thumbnails of six hundred pictures of a bird. You hit Cancel, pick the right folder, and a kingfisher pops up among the wedding guests. The first import has managed to outlive its own Cancel button.
This is one direction in programming I’m excited about. More of the promises we used to leave in comments can go in the code itself. Take that import apart and you can see it happening in three languages. JavaScript can close the file for you. Python can account for the whole flock of jobs. Swift can catch two parts of the app trying to edit the same bird.
How long do we need the file?#
Say the importer reads a photo into memory and hands the bytes to a decoder. At that point the app has everything it needs from the SD card. Here’s the whole file-reading function in Node.js.
import { open } from "node:fs/promises";async function readPhoto(path) { await using file = await open(path, "r"); return await file.readFile();}There are two awaits on the line that opens the file. The one on the right waits for the file to open. The one in await using tells JavaScript to wait for it to close on the way out. By the time the caller gets the bytes, the handle is closed. Add an early return next month for an unsupported photo format and the file still gets closed. Node made file handle disposal stable in 24.2.
Now pull the card halfway through a read. Suppose the read fails and closing the file throws a second error. Which one should the caller see? A plain finally that throws would hide the read error. JavaScript’s disposal machinery builds a SuppressedError that keeps both. If the block opened several resources, it still tries to dispose of the rest, most recently acquired first. That’s a surprising amount of error handling to get out of one line. TC39’s disposal algorithm.
What does the Cancel button wait for?#
Closing a file does nothing to the thumbnails already under way. Imagine a Python version of the importer that fetches a handful of photos from a network camera. We can give each fetch its own task and put them all in one asyncio.TaskGroup. Python 3.15 added a particularly nice method for our button. The UI can call jobs.cancel() on the event loop that runs this import.
async def import_batch(jobs, photos): async with jobs: for photo in photos: jobs.create_task(fetch_preview(photo))Here jobs is a fresh task group shared with the button. When the user cancels, the group asks each unfinished task to stop. A fetch suspended at an await can receive that request, release its connection in a finally block, and finish. Only after the last child finishes can execution pass the end of async with. That gives the UI a sensible moment to reuse the thumbnail tray. A child spinning in a long loop can still make the user wait.
The fiddly case is when two things go wrong at once. A fetch fails, so the group starts cancelling its siblings. Meanwhile the app is quitting and cancels the import from outside. The group uses cancellation internally to wake its parent. If it swallows the outside request along with its own wake-up, the app can miss the instruction to quit. Python 3.13 improved how groups keep track of those overlapping requests. Python’s task group documentation.
Who gets to change the pixels?#
One photo makes it through. In a Swift app, the decoder fills a mutable image buffer, then passes it to an actor that owns the preview tray. An actor lets one piece of code at a time touch its isolated state. That buys us very little if the decoder kept a spare reference and uses it to draw a watermark while the tray is reading the pixels.
Two references point to the same mutable pixel buffer.
- Decoder: spare points to Pixel buffer
- Preview tray: image points to Pixel buffer
- Pixel buffer: first pixel: olive
Swift 6 gives the tray a way to ask for a handoff. Its method can take _ image: sending PixelBuffer. With a fresh instance of this mutable class, the decoder can fill the buffer, pass it in, and go on to the next photo. You just handed over a twelve-megapixel bird without copying it. If the decoder later writes through spare, Swift can flag the handoff at compile time. Swift’s sending proposal.
Finding the spare is the hard part. It might live inside another object, or get assigned only in the branch that adds watermarks. Swift’s compiler groups together values that might reach the same mutable storage and tracks those groups through branches. Sometimes it has to assume two values are connected even when the programmer knows they’re separate. That’s the price of checking the handoff before a single photo has loaded. Swift’s region analysis.
I’d still keep a test for that stray kingfisher. A thumbnail might already be sitting in the UI’s event queue when Cancel lands. Even a perfectly behaved download can leave one last bird in the queue. Give each import a number. When a thumbnail arrives for import 17 and the wedding is import 18, drop the bird.