You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
when copying an Fs implementation, its Features() should not retain the original receiver #2182
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 newFs with an adjusted root folder. When doing so, it copies the already filled Features from the originalFs.
Consider this pseudo code to get an idea of the bug:
f:=&Fs{name: "old"}
f.features= (&fs.Features{}).Fill(f)
newF:=*fnewF.name="new"doPrintName:=newF.Features().PrintNamefmt.Println(f.Name, newF.name, doPrintName())
// prints "old new old"
resolve the methods in Features() whenever needed instead of once through Fill() (?)
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.
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 :)
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.
resolve the methods in Features() whenever needed instead of once through Fill() (?)
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.
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
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.
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.
Activity
Merovius commentedon Mar 26, 2018
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
and
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 :)
ncw commentedon Mar 27, 2018
@Merovius
I appreciate you dropping by and thank you for making that brilliant/disgusting function! I note your warning about the unsafe magic.
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 Styleway 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
I think we've discussed 1 already. 2 Is an interesting idea and could work and be abstracted out into a helper function probably.
Merovius commentedon Mar 27, 2018
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() Fsthat 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.
drive: when initialized with a filepath, optional features used incor…
breunigs commentedon Mar 28, 2018
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.
acd,box,onedrive,opendrive,ploud: fix Features() retaining the origin…
drive: when initialized with a filepath, optional features used incor…
acd,box,onedrive,opendrive,ploud: fix Features() retaining the origin…