Skip to content

when copying an Fs implementation, its Features() should not retain the original receiver #2182

Closed

Description

@breunigs

This is a spin-off from #2176 because we realized this is an unrelated issue.

For instance, this bug hits when in the newFs of Google Drive whenever a path to a file is given. The code realizes this and creates a new Fs with an adjusted root folder. When doing so, it copies the already filled Features from the original Fs.

Consider this pseudo code to get an idea of the bug:

f := &Fs{name:  "old"}
f.features = (&fs.Features{}).Fill(f)

newF := *f
newF.name = "new"

doPrintName := newF.Features().PrintName 

fmt.Println(f.Name, newF.name, doPrintName())

// prints "old new old"

There's many options to avoid that:

  1. ensure in a test that &someF == getReceiver(someF.Features().someOptionalFeature). It depends on implementation details, though. See e.g. https://play.golang.org/p/so-l-ZSPCSo (license unknown)
  2. resolve the methods in Features() whenever needed instead of once through Fill() (?)
  3. attempt to replace the Features() approach by making all optional interfaces mandatory and return errNotImplemented when a backend doesn't support them

I have not yet looked into the feasibility of either. Maybe there's even a better solution I'm overlooking.

In the short term, I will provide a patch that fixes the specific bug for the Drive backend.

Activity

  1. Merovius commented on Mar 26, 2018

    @Merovius

    See e.g. https://play.golang.org/p/so-l-ZSPCSo (license unknown)

    For the record: When I wrote this, I intended it more as an illustration of why this is impossible to do reliably, not to actually use it :) I don't mind if you end up using it, but I would advise against it - even in tests ;)

    The main risk here is that future changes (especially around heuristics for inlining and devirtualization) may make this method change behavior based on the context it's called in. This might cause non-deterministic, hard to trace and to debug test failures. Imagine a randomly failing test, where the location from the stacktrace is not related to the actual bug location, which is not reproducible in a debugger (which compiles with different optimizations) and whose root cause you can't write a test for, because that test might pass, while the original one still fails (because the optimization decisions are based on context). It may seem unlikely to break this way, but the pain it would cause seems immense to me :)

    If it helps, strings.Builder uses a trick to detect copies-by-value by comparing a self-pointer; would that be useful to maybe prevent copying of fs.Features? Though it seems they get, in general, used as a pointer, so that probably doesn't help…

    Personally, I believe I would recommend making the optional features their own interface and then do type-assertions at the use-site. There don't seem to be a significant syntactical difference between

    if ps, ok := f.(PutStreamer); ok {
        ps.PutStream(foobar)
    }
    // or
    f.(PutStreamer).PutStream(foobar)
    

    and

    if ps := f.Features().PutStream; ps != nil {
        ps(foobar)
    }
    // or
    f.Features().PutStream(foobar)
    

    I'd guess that similar considerations also apply to the ErrNotImplemented idea - in that case, a conditional with a type-assertion is replaced by a conditional checking for that error. In the end, I don't think these three have very significant differences in syntactical boilerplate.

    The optional interface approach, though, at least gives the option to use them in a signature, if a function actually needs them to work. It would open up the possibility of moving optional-feature specific code into its own function with a more specific type-signature, meaning you only have to do the runtime-checks at the outermost API layer and then get the compiler to help you.

    I'm just driving through, so my opinion isn't particularly important, but I do wanted to emphasize that I think the costs of the unsafe magic I devised are easy to underestimate :)

  2. ncw commented on Mar 27, 2018

    @ncw
    Member

    @Merovius

    I appreciate you dropping by and thank you for making that brilliant/disgusting function! I note your warning about the unsafe magic.

    Personally, I believe I would recommend making the optional features their own interface and then do type-assertions at the use-site. There don't seem to be a significant syntactical difference between

    That is exactly how they used to work, but the trouble with that is that Fs can wrap other Fs.

    So the outer Fs has to implement all the optional features but the inner Fs may not support them.

    rclone used to have a system of special error codes (not exactly like ErrNotImplemented but a different error message for each interface - similar enough) which the outer Fs would return to mean "Not implemented" if the inner Fs didn't support it. However knowing something doesn't exist after you call it really complicates the code.

    What I'd really like is a way of dynamically adding and removing methods to the outer Fs depending on the methods available on the inner Fs. Then everything would work perfectly with interfaces. However I couldn't find a way of making that work, so I went with the C Style way of using function pointers.

    I'd be very interested to hear if you have a better solution as it has been bugging me for ages! It kind of seems like a wart of Go to me.

    @breunigs wrote

    1. ensure in a test that &someF == getReceiver(someF.Features().someOptionalFeature). It depends on implementation details, though. See e.g. https://play.golang.org/p/so-l-ZSPCSo (license unknown)
    2. resolve the methods in Features() whenever needed instead of once through Fill() (?)
    3. attempt to replace the Features() approach by making all optional interfaces mandatory and return errNotImplemented when a backend doesn't support them

    I think we've discussed 1 already. 2 Is an interesting idea and could work and be abstracted out into a helper function probably.

    1. is almost what rclone used to do, except it would assert the optional interface first, then call it, then do something if it wasn't implemented. Here is the commit where it changed: 1fa258c
  3. Merovius commented on Mar 27, 2018

    @Merovius

    That is exactly how they used to work, but the trouble with that is that Fs can wrap other Fs.

    Ah yes, I am aware of the general problem but didn't make that connection here. Unfortunately, I don't believe there is a good solution for this given the language as it stands. It's been bugging me too :)

    FWIW, I think extraction-methods would be my favorite if I had to solve this - i.e. add an (optional?) Wrapped() Fs that should be implemented if you are wrapping something and then search the (sort of) stack for an implementation of an optional interface. But yes, it's definitely also not a good solution.

    So, ignore that part of my comment, sorry for the noise :) Wasn't aware of the full extent of this.

  4. breunigs commented on Mar 28, 2018

    @breunigs
    ContributorAuthor

    I spent some time on trying to write a test case, using the snippet @Merovius thankfully provided. Unfortunately it returns different memory addresses, even when I am quite sure the inspected function works on the right receiver. I don't understand Go well enough yet to debug why this is happening. The test cases in #2176 cover this at least partially, which is how I found this to begin with. Not ideal, bet definitely better than nothing.

  5. added this to the v1.44 milestone on Oct 14, 2018
  6. added a commit that references this issue on Oct 14, 2018
    0f2a540
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions