safe-read icon indicating copy to clipboard operation
safe-read copied to clipboard

Lib needs a way to force reset of Buffer cache

Open dbmcclain opened this issue 3 years ago • 1 comments

Calling SAFE-READ from within a WITH-INPUT-FROM-STRING causes cache confusion when faced with later, similar, string-streams after earlier ones have gone out of scope. Depending on the Lisp implementation, this problem might also arise with other streams of dynamic extent.

There is no way for the library to know if the stream ref handed to it is of dynamic extent, so it is incumbent on the caller to arrange for the cache to be cleared after using the SAFE-READ function in dynamic frames, to prevent any dangling references remaining in the cache. Something like a SAFE-READ-CLEAR-CACHE in an UNWIND clause surrounding the calls to SAFE-READ.

Or perhaps you just need a separate entry point, SAFE-READ-FROM-STRING, expressly tailored for string input of complete expressions. Then you are in control of your own cache, and in this case, you won't need any cache. But this might still be problematic for other potential dynamic-extent streams, if such exist.

I guess the problem arises because your code is so useful for many situations, but you originally intended only interactive stream input? Any other kind of input stream would need no cache. So maybe just a SAFE-READ-FROM-NONINTERACTIVE-STREAM? or a flag (:INTERACTIVE T) that could be set to NIL by the caller of SAFE-READ.

Your cache is your business, and users are initially unaware of its existence, and they should not be poking into your logic from afar. But without these augmentations it becomes potentially unsafe for any but interactive users.

dbmcclain avatar Oct 10 '22 20:10 dbmcclain

While I proposed inventing a new non-interactive mode to avoid the use of cached stream refs that may be DX (as in arriving via WITH-INPUT-FROM-STRING), another, possibly better approach would be to make your SAFE-READ into a CLOS Method that selects on the first argument being either a STREAM or a STRING.

In the case of STRING, you just check the length to be sure it is within limits, remove leading whitespace and check that the first char is open-parenthesis, and then call Lisp READ-FROM-STRING directly while under control of your limited readtable.

dbmcclain avatar Oct 11 '22 13:10 dbmcclain