From 2c3eaf51283feaa7ef65a15ff7c5f95edcbc7d5c Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 17 Nov 2026 13:38:58 +0100 Subject: [PATCH] vm(images): Image.open takes no block, and wiring one would call a filter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #950's second half asked whether Images::Image.open discarding its block is a gap, the way the socket doors' discarded blocks were (#931/#932: 27 hung spec files) and Augeas.open's was (#952). The row in testdata/open-doors.txt said "UNSPECIFIED, not a known defect" because nobody had read the reference. Read now. go-ruby-images' README calls itself "a MiniMagick / ruby-vips replacement", and MiniMagick's lib/mini_magick/image.rb declares: def self.open(path_or_url, ext = nil, **options) with NO block parameter — no `yield`, no `block_given?`, no `ensure`. The only blocks in its body are internal, handed to `uri.open` and `pathname.open`. ruby-vips has no `Image.open` at all; it spells this `Vips::Image.new_from_file`. A Ruby method that takes no block and is handed one ignores it, so rbgo is already right. Nothing to fix. ⛔⛔ AND THE "FIX" WOULD BE WORSE THAN POINTLESS. socketOpenWithBlock closes through the CLASS'S OWN `close`, and on Images::Image `close` is the MORPHOLOGICAL CLOSING operator — dilate then erode, taking a radius — exactly as `#open` is morphological opening. Wrapping the door would run an image filter on the way out of every block, believing it had released something. That is measured, not reasoned: wiring it makes the program die with ArgumentError: wrong number of arguments (given 0, expected 1) raised by that operator, called with no radius from the helper's defer. So three things land, none of them a behaviour change: - TestImagesImageOpenTakesNoBlock pins the contract. Without it the correct behaviour is INDISTINGUISHABLE from the defect it resembles: three other `open` doors that discarded a block were real bugs, so the next reader has every reason to assume a fourth. It asserts both halves a yield-only change would get wrong — the block does not run, and `open` answers the IMAGE, not the block's value. - the open-doors row is upgraded from UNSPECIFIED to reference-read, and says why the door STAYS listed: the row is what protects a correct door from being repaired. - socketOpenWithBlock's comment now states the assumption it was carrying silently — that `close` means "release" on the class being wrapped — with this counter-example. Nothing in the helper can tell the two meanings apart, since a native checks its own arguments and even arity is not introspectable; the door table is the check that can, so the instruction is to read what a new class's `close` does before wrapping it. ⛔ What I did NOT file, having looked: rbgo's `Image.open` takes one argument where MiniMagick takes `(path_or_url, ext = nil, **options)`, and `imagesArity` is a MINIMUM, so a second argument is silently ignored rather than refused. That is cosmetic for decoding — go-ruby-images' Open calls image.Decode, which SNIFFS the format from content via the registered PNG/JPEG/GIF/BMP/TIFF/WebP decoders, so the `ext` hint changes nothing either way. The URL half is a real gap (MiniMagick fetches http/https/ftp) and is #957 rather than smuggled in here, because implementing it gives the interpreter an outbound fetch driven by a path-shaped argument — a judgement for the maintainer, not a parity checkbox. Verified by mutation, not by reading: wiring the door red-lines BOTH gates independently — the behavioural test, and the ratchet, which reports the row as one that no longer needs one. Reverted, both green. gofmt clean, buildscan --vet under all 19. Closes #950 Refs #957, #931, #932, #952 Co-Authored-By: Claude Opus 5 --- internal/vm/images.go | 16 ++++++++ internal/vm/images_internal_test.go | 57 +++++++++++++++++++++++++++++ internal/vm/socket.go | 15 ++++++++ internal/vm/testdata/open-doors.txt | 10 +++-- 4 files changed, 94 insertions(+), 4 deletions(-) diff --git a/internal/vm/images.go b/internal/vm/images.go index 3464f2a0..032ed070 100644 --- a/internal/vm/images.go +++ b/internal/vm/images.go @@ -89,6 +89,22 @@ func (vm *VM) registerImagesImage(mod *RClass) { vm.consts["Images::Image"] = cls // Images::Image.open(path) decodes the image file at path. + // + // ⛔ NOT socketOpenWithBlock, AND IT MUST NOT BECOME IT. Two reasons, and the + // second is the dangerous one: + // + // 1. MiniMagick — what go-ruby-images' README says it replaces — declares + // `def self.open(path_or_url, ext = nil, **options)` with no block + // parameter at all, so there is no block form to honour (#950). + // 2. That helper closes by sending the class's OWN `close`, and on this + // class `close` is the MORPHOLOGICAL CLOSING operator — dilate then + // erode, taking a radius (see registerImagesTransforms). So the wiring + // would call an image filter on the way out of the block, believing it + // was releasing a resource. `Images::Image#open` is the morphological + // opening for the same reason; the collision is on both names. + // + // TestImagesImageOpenTakesNoBlock and testdata/open-doors.txt both go red if + // this is "fixed". cls.smethods["open"] = &Method{name: "open", owner: cls, native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { imagesArity(args, 1) diff --git a/internal/vm/images_internal_test.go b/internal/vm/images_internal_test.go index b12abc7f..c083eb2c 100644 --- a/internal/vm/images_internal_test.go +++ b/internal/vm/images_internal_test.go @@ -177,6 +177,63 @@ puts r.join("|") } } +// TestImagesImageOpenTakesNoBlock pins a NON-defect, which is the only reason it +// exists. +// +// #950 asked whether Images::Image.open discarding its block was a gap, the way +// the socket doors' discarded blocks were (#931/#932: 27 hung spec files) and +// the way Augeas.open's was (#952). Reading the reference settles it the other +// way. go-ruby-images' README calls itself "a MiniMagick / ruby-vips +// replacement", and MiniMagick's is: +// +// def self.open(path_or_url, ext = nil, **options) +// +// with no block parameter — no `yield`, no `block_given?`, no `ensure`. The only +// blocks in its body are internal, handed to `uri.open` / `pathname.open`. +// ruby-vips has no `Image.open` at all; it spells this `Vips::Image +// .new_from_file`. A Ruby method that takes no block and is handed one ignores +// it, so rbgo discarding it IS the contract. +// +// ⛔ WITHOUT THIS TEST THE CORRECT BEHAVIOUR IS INDISTINGUISHABLE FROM THE +// DEFECT IT RESEMBLES. The three other `open` doors that discarded a block were +// all real bugs — #931/#932 for the sockets, #952 for Augeas — so the next +// reader to notice this one has every reason to assume the same and to "fix" it +// into socketOpenWithBlock. +// +// ⛔⛔ AND THAT FIX WOULD BE WORSE THAN POINTLESS, which is what the mutation +// showed rather than reasoned: socketOpenWithBlock closes through the CLASS'S +// OWN `close`, and on Images::Image `close` is the MORPHOLOGICAL CLOSING +// operator — dilate then erode, taking a radius — just as `#open` is +// morphological opening. Wiring the door makes the program die with +// +// ArgumentError: wrong number of arguments (given 0, expected 1) +// +// from that operator, called with no radius on the way out of the block. The +// helper's "close whatever the class calls close" contract is silent about +// classes where that name means something else, and this is one. +func TestImagesImageOpenTakesNoBlock(t *testing.T) { + dir := t.TempDir() + src := fmt.Sprintf(` +require "images" +Images::Image.read(Images::Canvas.new(2, 2, Images::Color::WHITE).to_blob).write(%q) + +ran = false +got = Images::Image.open(%q) { |i| ran = true; "the block's value" } + +# The block is not called, and open answers the IMAGE -- not the block's value, +# which is the half a yield-only "fix" would still get wrong. +puts ran.inspect +puts got.class +puts got.dimensions.inspect +`, dir+"/in.png", dir+"/in.png") + want := "false\nImages::Image\n[2, 2]" + if got := runSrc(t, src); got != want { + t.Errorf("Image.open with a block:\n got=%q\nwant=%q\n"+ + "a block that RAN, or a return value that is not the image, means this door was wired to "+ + "socketOpenWithBlock -- see testdata/open-doors.txt: MiniMagick's open takes no block", got, want) + } +} + // TestImagesImageErrors covers every Images::Image error branch: a missing file // and a corrupt blob (imagesWrap -> Images::Error), a non-positive resize and an // unencodable convert (also imagesWrap), a bad rotation, an out-of-bounds pixel diff --git a/internal/vm/socket.go b/internal/vm/socket.go index 0de8ef39..4edf4f90 100644 --- a/internal/vm/socket.go +++ b/internal/vm/socket.go @@ -233,6 +233,21 @@ func (vm *VM) installSocketNonblock() { // connection, so there is one close per class instead of a second spelling of // it; each of those is already idempotent, so a block that closed the socket // itself does not get it closed twice. +// +// ⛔ THAT CARRIES AN UNSTATED ASSUMPTION: that on the class being wrapped, +// `close` MEANS "release the resource". It is not true of every class in this +// package. Images::Image has a `close` — the morphological closing operator, +// dilate then erode, taking a radius — and an `open` that is morphological +// opening, so wrapping Images::Image.open here would run an image filter on the +// way out of the block believing it had released something; measured, the +// program dies with `wrong number of arguments (given 0, expected 1)` from that +// operator (#950, images.go). +// +// Nothing here can tell the two meanings apart — a native checks its own +// arguments, so even arity is not introspectable. The check that can is +// testdata/open-doors.txt plus TestEveryOpenDoorIsAccountedFor: every `open` +// door is either wrapped here or carries a row saying why not. Before wrapping +// a new class, read what ITS `close` does. func socketOpenWithBlock(cls *RClass, newFn NativeFn) NativeFn { return func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { v := newFn(vm, self, args, nil) diff --git a/internal/vm/testdata/open-doors.txt b/internal/vm/testdata/open-doors.txt index 46a5779d..f6180133 100644 --- a/internal/vm/testdata/open-doors.txt +++ b/internal/vm/testdata/open-doors.txt @@ -27,9 +27,11 @@ net_ftp.go:ftp Net::FTP.open yields the connected client through ftpWithClose, w sqlite3.go:cls SQLite3::Database.open yields and closes the database in a defer (sqlite3.go:102); SQLite3::Database.new shares the same func. bolt.go:cls The bbolt database's open yields and closes the handle (bolt.go:76), returning the block's value. -# ───────── does NOT honour its block. #950 -# Takes `_ *Proc` and discards it. Listed here rather than fixed so the count is -# visible: one door, on a binding surface, not in MRI core. +# ───────── discards its block, and that is the CONTRACT. #950 +# One door, on a binding surface, not in MRI core. It was listed here as +# "UNSPECIFIED, not a known defect" while nobody had read the reference. The +# reference has now been read and it settles the question: there is no block +# form to honour, so a discarded block is Ruby behaving normally, not a gap. # # ⛔ augeas.go:cls WAS THE OTHER ONE, and its row said the fix was "blocked" # because rbgo's Augeas had no #close for socketOpenWithBlock to call and @@ -39,4 +41,4 @@ bolt.go:cls The bbolt database's open yields and closes the handle (bolt.go:76), # guard, needing nothing from the dependency. The door now goes through # socketOpenWithBlock, so it is correct by construction and owes this file no # row at all. -images.go:cls UNSPECIFIED, not a known defect. Images::Image.open discards its block (images.go:93). go-ruby-images' README documents no Ruby `Image.open` and no block form at all — it documents the Go library (`images.Open("photo.jpg")`) — and ruby-vips and MiniMagick spell this operation without a block, so "no block form" may be the contract to DOCUMENT rather than a gap to fill. See #950. +images.go:cls ⛔ NOT A DEFECT, reference READ. Images::Image.open discards its block (images.go:93), and MiniMagick — which go-ruby-images' README names as what it replaces, "a MiniMagick / ruby-vips replacement" — defines `def self.open(path_or_url, ext = nil, **options)` with NO block parameter: no `yield`, no `block_given?`, no `ensure`. The only blocks in its body are internal, passed to `uri.open` / `pathname.open`. ruby-vips has no `Image.open` at all (`Vips::Image.new_from_file`). A Ruby method that takes no block and is given one simply ignores it, so this door is correct as written. ⛔⛔ AND WIRING IT ANYWAY WOULD BE WORSE THAN POINTLESS: socketOpenWithBlock closes through the CLASS'S OWN `close`, and on Images::Image `close` is the MORPHOLOGICAL CLOSING operator — dilate then erode, taking a radius — just as `#open` is morphological opening. The helper would run an image filter on the way out of the block believing it had released something. Measured, not reasoned: wiring it makes the program die with `ArgumentError: wrong number of arguments (given 0, expected 1)` from that operator. ⛔ The row stays BECAUSE the door is correct — it is what keeps the next reader from "fixing" a door that looks exactly like the three real bugs (#931/#932 sockets, #952 Augeas). (MiniMagick's `self.create(ext, &block)` does yield a Tempfile to write into, but rbgo binds no `Images::Image.create`, so that is not a door here.) Settles #950.