jszwec / s3fs

S3 FileSystem (fs.FS) implementation
MIT License
177 stars 20 forks source link

Unsafe functional options #6

Closed sagikazarmark closed 2 years ago

sagikazarmark commented 2 years ago

I noticed you use functional options (which is great), but way they are currently implemented is not perfectly concurrent safe.

Here is the option type:

// Option is a function that provides optional features to S3FS.
type Option func(*S3FS)

The problem with this signature is that you can use options after initialization to modify the state of S3FS:

WithReadSeeker(New(...))

You can avoid that by introducing a private option struct:

// Option is a function that provides optional features to S3FS.
type Option func(*s3FSoptions)

An even better solution is making the Option type an interface:

// Option is a function that provides optional features to S3FS.
type Option interface{
    apply(*S3FS)
}

Using any of the above methods callers won't be able to modify internal state after initialization.

jszwec commented 2 years ago

Both of the proposed solution are not great

type Option func(*s3FSoptions)

You should not use unexported types args on exported functions. Even linters are picking these up

type Option interface{
    apply(*S3FS)
}

This is also a bad idea. You should not expose interfaces that cannot be implemented by anything from outside the package.

I don't think this is an issue at all. Look at Go's standard libraries, there are lots of options that are not safe for similar reason. Even JSON decoder... Usually things like this are dealt through documentation by saying something like "Options should be applied before the first use" At the end of the day, this is not a problem if you use it correctly and that's how many libraries even in Go standard library work today. If you want to misuse it then you are probably doing it on purpose, because why would anyone start a go routine and call WithOption() or do it in the middle of for loop

sagikazarmark commented 2 years ago

You should not expose interfaces that cannot be implemented by anything from outside the package.

That's exactly the point though: this interface should and never will be implemented or called outside of the package. (Yes, what you say makes sense in 99% of the cases...this is the exception)

Both of the above solutions are established patterns to prevent abuse of functional options.

The problem usually isn't the fact that people do it on purpose, but that fact that they don't know that's a problem. It's probably not that big of an issue here and won't cause any concurrency problems, but it's usually better to be on the defensive side and these patterns help with that.

Anyway, I reported the issue, up to you if you want to do anything about it or not. Feel free to close if you don't think it's a problem. Thanks for replying.

jszwec commented 2 years ago

That's exactly the point though: this interface should and never will be implemented or called outside of the package. (Yes, what you say makes sense in 99% of the cases...this is the exception)

Both of the above solutions are established patterns to prevent abuse of functional options.

I disagree with both.

Can you do it? yes Should you do it and it's idiomatic? no

Thanks for reporting but I don't think this is an issue. Every code and library can be misused and abused, we can't protect against everything