Repository navigation
Do not hold references to ALTREP data in Variables pane bindings - #1485
Merged
Merged
Conversation
juliasilge
marked this pull request as ready for review
November 17, 2026 15:21
samclark2015
approved these changes
Nov 17, 2026
samclark2015
left a comment
There was a problem hiding this comment.
Confirmed the unit test fails against R 4.6.2 without the fix, and passes with. LGTM!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 2when 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 keptR_altrep_data1()andR_altrep_data2()as protectedRObjectvalues. This protection increased the reference count of the wrapper data. The sequence from the issue is:W = drop(Z %*% X)makes a wrapper. The Variables pane keeps a reference to the data of the wrapper.a = W + 1gets 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.b = W + 2gets 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
data1anddata2are now rawSEXPpointers. The Variables pane uses them only to compare pointers inBindingValue::id(). Theobjectfield 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_dataincrates/harp/src/environment_iter.rskeeps aBindingto a wrapper while it uses the wrapper two times.test_variables_pane_does_not_share_altrep_wrapper_dataincrates/ark/tests/integration/kernel_variables.rsruns 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
Bug Fixes
REFCNT on locked WRAPPER data increased to 2when the Variables pane is open (REFCNT on locked WRAPPER data increased to 2error when using Positron with R 4.6.2 positron#17279)