pb33f/libopenapi-validator

Concurency safe validators?

buni opened this issue · 11 comments

buni commented

I've a use case where concurrency safety is needed (a middleware), and I noticed that validator instances are not concurrency safe. I wasn't sure if it was by design?
I think the library would be more useful if validators were concurrency safe. I'm happy to do the work myself if outside contributions are accepted.

Would you mind showing me the issue / error you see when running the validator as middleware? There should be no mutations, only internal state changes.

The goal is to be concurrency safe, any help you can provide to show me where the library gets upset and I can fix it.

buni commented

I've a couple of basic test cases for the race conditions I found https://go.dev/play/p/Y1CUxCwOjPG
In addition all SetPathItem methods are not concurrency safe.

Can you try v0.0.20 and see how you get on? This addresses some other concurrency issues reported in the stack.

buni commented

I'm still getting race conditions when running my test cases.

buni commented

This branch should have all data races that I found fixed, there might be more since my tests were very basic. Feel free to use that code as a pointer, I quickly threw it together when I was initially testing your library so its no the pretties. Also you might want to run a linter, golangci-lint found some potential issues when I ran it on the project.

Thank you, I will circle back on this ASAP and do another concurrency cleanup.

Sakdow commented

Hey @daveshanley , any news on this ? Got concurrency problems in my tests on version 0.0.37:

fatal error: concurrent map writes

goroutine 337 [running]:
github.com/pb33f/libopenapi-validator/requests.(*requestBodyValidator).ValidateRequestBody(0x14000033440, 0x140002b0500)
	/go/pkg/mod/github.com/pb33f/libopenapi-validator@v0.0.37/requests/validate_body.go:80 +0x404
github.com/pb33f/libopenapi-validator.(*validator).ValidateHttpRequest.func2(0x0?, 0x0?)
	/go/pkg/mod/github.com/pb33f/libopenapi-validator@v0.0.37/validator.go:247 +0x3c
created by github.com/pb33f/libopenapi-validator.(*validator).ValidateHttpRequest in goroutine 436
	/go/pkg/mod/github.com/pb33f/libopenapi-validator@v0.0.37/validator.go:267 +0x2f0

This is a new one. I think it can be solved pretty easily.

@Sakdow can you try v0.0.38

Sakdow commented

Sorry for the late reply. It's working perfectly now, thank you!

I can't detect any race conditions in tests or in production use. Until this comes back around, closing for now.