]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix UseSources panicking when sqlite storage is closed
authorMatt Joiner <anacrolix@gmail.com>
Fri, 23 Jun 2023 12:14:38 +0000 (22:14 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 23 Jun 2023 12:14:38 +0000 (22:14 +1000)
go.mod
go.sum
storage/interface.go
storage/sqlite/direct.go
storage/test/bench-piece-mark-complete.go
test/misc_test.go [new file with mode: 0644]

diff --git a/go.mod b/go.mod
index f0eadcc230aaf29056a2727b8a80a27c28660fd1..2e19887bdb980c0e030236f6832e265931783620 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -18,7 +18,7 @@ require (
        github.com/anacrolix/missinggo/perf v1.0.0
        github.com/anacrolix/missinggo/v2 v2.7.2-0.20230527121029-a582b4f397b9
        github.com/anacrolix/multiless v0.3.0
-       github.com/anacrolix/squirrel v0.4.1-0.20220122230132-14b040773bac
+       github.com/anacrolix/squirrel v0.4.1-0.20230623120945-75cf0ad9a958
        github.com/anacrolix/sync v0.4.0
        github.com/anacrolix/tagflag v1.3.0
        github.com/anacrolix/upnp v0.1.3-0.20220123035249-922794e51c96
diff --git a/go.sum b/go.sum
index 5ed1f2bfe85a1dc937e137b1713df211c76d9366..28ef842a5c51ee6336fbee665041cb3b6d3cf90c 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -103,8 +103,8 @@ github.com/anacrolix/mmsg v1.0.0 h1:btC7YLjOn29aTUAExJiVUhQOuf/8rhm+/nWCMAnL3Hg=
 github.com/anacrolix/mmsg v1.0.0/go.mod h1:x8kRaJY/dCrY9Al0PEcj1mb/uFHwP6GCJ9fLl4thEPc=
 github.com/anacrolix/multiless v0.3.0 h1:5Bu0DZncjE4e06b9r1Ap2tUY4Au0NToBP5RpuEngSis=
 github.com/anacrolix/multiless v0.3.0/go.mod h1:TrCLEZfIDbMVfLoQt5tOoiBS/uq4y8+ojuEVVvTNPX4=
-github.com/anacrolix/squirrel v0.4.1-0.20220122230132-14b040773bac h1:eddZTnM9TIy3Z9ARLeDMlUpEjcs0ZdoFMXSG0ChAHvE=
-github.com/anacrolix/squirrel v0.4.1-0.20220122230132-14b040773bac/go.mod h1:YzgVvikMdFD441oTWlNG189bpKabO9Sbf3uCSVgca04=
+github.com/anacrolix/squirrel v0.4.1-0.20230623120945-75cf0ad9a958 h1:A+tNxHKFCGj/CP8WaQDZC+QwDjjoXUHDByIzKVyzKw4=
+github.com/anacrolix/squirrel v0.4.1-0.20230623120945-75cf0ad9a958/go.mod h1:YzgVvikMdFD441oTWlNG189bpKabO9Sbf3uCSVgca04=
 github.com/anacrolix/stm v0.2.0/go.mod h1:zoVQRvSiGjGoTmbM0vSLIiaKjWtNPeTvXUSdJQA4hsg=
 github.com/anacrolix/stm v0.4.0 h1:tOGvuFwaBjeu1u9X1eIh9TX8OEedEiEQ1se1FjhFnXY=
 github.com/anacrolix/stm v0.4.0/go.mod h1:GCkwqWoAsP7RfLW+jw+Z0ovrt2OO7wRzcTtFYMYY5t8=
index b28a52b0efb8cfa1d47556e4e461c6e4a9caebdf..9e8de06c8f55313370e938d386f234f45b22e3d3 100644 (file)
@@ -51,6 +51,7 @@ type PieceImpl interface {
 type Completion struct {
        Complete bool
        Ok       bool
+       Err      error
 }
 
 // Allows a storage backend to override hashing (i.e. if it can do it more efficiently than the torrent client can)
index 3d51fd316b27385948feab321fc9015d48038c8d..bd656e60a389c05b670c98e2ec4a0da1daabc1ba 100644 (file)
@@ -79,8 +79,6 @@ func (p piece) Completion() (ret storage.Completion) {
                ret.Complete = stmt.ColumnInt(0) != 0
        })
        ret.Ok = err == nil
-       if err != nil {
-               panic(err)
-       }
+       ret.Err = err
        return
 }
index 71b27afccf6d217eb1f6ad9a8a0cc0b1be369956..db0049f22674b98ff499bd12745799133d01869a 100644 (file)
@@ -66,7 +66,7 @@ func BenchmarkPieceMarkComplete(
                        // This might not apply if users of this benchmark don't cache with the expected capacity.
                        c.Assert(pi.Completion(), qt.Equals, storage.Completion{Complete: false, Ok: true})
                        c.Assert(pi.MarkComplete(), qt.IsNil)
-                       c.Assert(pi.Completion(), qt.Equals, storage.Completion{true, true})
+                       c.Assert(pi.Completion(), qt.Equals, storage.Completion{Complete: true, Ok: true})
                        n, err := pi.WriteTo(bytes.NewBuffer(readData[:0]))
                        b.StopTimer()
                        c.Assert(err, qt.IsNil)
diff --git a/test/misc_test.go b/test/misc_test.go
new file mode 100644 (file)
index 0000000..a72746c
--- /dev/null
@@ -0,0 +1,54 @@
+package test
+
+import (
+       "net"
+       "net/http"
+       "testing"
+
+       qt "github.com/frankban/quicktest"
+
+       "github.com/anacrolix/torrent"
+       "github.com/anacrolix/torrent/bencode"
+       "github.com/anacrolix/torrent/metainfo"
+       sqliteStorage "github.com/anacrolix/torrent/storage/sqlite"
+)
+
+func TestUseSourcesSqliteStorageClosed(t *testing.T) {
+       c := qt.New(t)
+       cfg := torrent.TestingConfig(t)
+       storage, err := sqliteStorage.NewDirectStorage(sqliteStorage.NewDirectStorageOpts{})
+       defer storage.Close()
+       cfg.DefaultStorage = storage
+       c.Assert(err, qt.IsNil)
+       cl, err := torrent.NewClient(cfg)
+       c.Assert(err, qt.IsNil)
+       defer cl.Close()
+       l, err := net.Listen("tcp", "localhost:0")
+       c.Assert(err, qt.IsNil)
+       defer l.Close()
+       // We need at least once piece to trigger a call to storage to determine completion state.
+       i := metainfo.Info{Pieces: make([]byte, metainfo.HashSize)}
+       mi := metainfo.MetaInfo{}
+       mi.InfoBytes, err = bencode.Marshal(i)
+       c.Assert(err, qt.IsNil)
+       s := http.Server{
+               Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+                       mi.Write(w)
+               }),
+       }
+       defer s.Close()
+       go func() {
+               err := s.Serve(l)
+               if err != http.ErrServerClosed {
+                       panic(err)
+               }
+       }()
+       // Close storage prematurely.
+       storage.Close()
+       tor, _, err := cl.AddTorrentSpec(&torrent.TorrentSpec{
+               InfoHash: mi.HashInfoBytes(),
+               Sources:  []string{"http://" + l.Addr().String()},
+       })
+       c.Assert(err, qt.IsNil)
+       <-tor.GotInfo()
+}