Skip to content

Do not hold references to ALTREP data in Variables pane bindings - #1485

Merged
juliasilge merged 1 commit into
mainfrom
fix-altrep-wrapper-refcnt
Nov 17, 2026
Merged

juliasilge merged 1 commit into
mainfrom
fix-altrep-wrapper-refcnt

Conversation

@juliasilge

Copy link
Copy Markdown
Member

Addresses posit-dev/positron#17279

On R 4.6.2, R code can fail in the Positron console with REFCNT on locked WRAPPER data increased to 2 when the Variables pane is open. This change removes the cause, which turns out be an extra reference that the Variables pane holds to the data inside ALTREP wrapper objects.

R uses wrapper objects for some vectors of length 64 or more, for example the result of drop() on a matrix. A wrapper holds the attributes, and its data is a separate vector. When R gets a data pointer from a wrapper, R locks the wrapper. R 4.6.2 adds a check so that after the lock, the data of the wrapper must have only one reference. If the reference count increases, R gives the error that we see reported in the issue.

The Variables pane keeps the bindings of the global environment between top-level commands, to find the variables that change. For an ALTREP binding, Binding::new() also kept R_altrep_data1() and R_altrep_data2() as protected RObject values. This protection increased the reference count of the wrapper data. The sequence from the issue is:

  1. W = drop(Z %*% X) makes a wrapper. The Variables pane keeps a reference to the data of the wrapper.
  2. a = W + 1 gets a data pointer. The data is shared, so R makes a copy and locks the wrapper. Then the Variables pane keeps a reference to the new data.
  3. b = W + 2 gets a data pointer again. The locked data now has two references, and R gives the error.

When the user runs all lines as one command, the Variables pane does not update between the lines, so the error does not occur.

The fix

data1 and data2 are now raw SEXP pointers. The Variables pane uses them only to compare pointers in BindingValue::id(). The object field continues to protect the wrapper itself. The reference count of the wrapper has no effect on the R 4.6.2 check.

There is one small risk here, I think. If an ALTREP object replaces its data, R can free the old data and use the same address again for new data. In this case, the Variables pane can miss one update for that variable. I think that this is pretty unlikely to happen IRL, and the result is just an old value in the Variables pane, not an error.

Tests

  • test_binding_does_not_share_altrep_data in crates/harp/src/environment_iter.rs keeps a Binding to a wrapper while it uses the wrapper two times.
  • test_variables_pane_does_not_share_altrep_wrapper_data in crates/ark/tests/integration/kernel_variables.rs runs the steps from the issue as separate execute requests, with the Variables comm open.

Without the fix, both tests fail on R 4.6.2 with the error from the issue. On R versions before 4.6.2, the check does not exist, so both tests pass with or without the fix.

Positron Release Notes

New Features

  • N/A

Bug Fixes

@juliasilge
juliasilge marked this pull request as ready for review November 17, 2026 15:21

@samclark2015 samclark2015 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed the unit test fails against R 4.6.2 without the fix, and passes with. LGTM!

@juliasilge
juliasilge merged commit 13b20aa into main Nov 17, 2026
19 checks passed
@juliasilge
juliasilge deleted the fix-altrep-wrapper-refcnt branch November 17, 2026 17:11
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 17, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants