# Thread safety issue in WebDAV syncing?

**URL:** <https://forum.rclone.org/t/thread-safety-issue-in-webdav-syncing/43050>\
**Category:** Suspected Bug\
**Created:** [November 24, 2023, 3:05am UTC](https://forum.rclone.org/t/thread-safety-issue-in-webdav-syncing/43050 "2023-11-24T03:05:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![wxh](https://forum.rclone.org/user_avatar/forum.rclone.org/wxh/32/14865_2.png) [@wxh](https://forum.rclone.org/u/wxh)\
**Post date:** [November 24, 2023, 3:05am UTC](https://forum.rclone.org/t/thread-safety-issue-in-webdav-syncing/43050/1 "2023-11-24T03:05:08Z")

</div>

#### What is the problem you are having with rclone?

I've integrated `rclone` into my own app, for syncing files from a local computer to a WebDAV service on another computer in the same local network.

The sync is per file based (so I have more control on the sync'ing process and stats collecting). All is good if the syncs are done sequentially.

However it is too slow due to the handshake cost. So I decied to create a goroutine pool, and run the syncs concurrently (throttled at 4 goroutines)

Then the problem comes - the code is keep getting errors like

```auto
2023/11/24 10:52:45 ERROR : instrumenter.js: corrupted on transfer: sizes differ 1704 vs 0
2023/11/24 10:52:45 ERROR: failed to sync file resources\app\node_modules\better-ajv-errors\node_modules\core-js\web\dom-collections.js to device: corrupted on transfer: sizes differ 1704 vs 0

```

It looks like a thread safety issue somewhere in the `rclone` code. If anyone can give some guidance, I am happy to dig deeper.

#### Run the command 'rclone version' and share the full output of the command.

Copied from my `go.mod`:

```auto
module myapp

go 1.20

require (
	...
	github.com/rclone/rclone v1.61.1
	...
)

```

#### Which cloud storage system are you using? (eg Google Drive)

WebDAV

#### The command you were trying to run (eg `rclone copy /tmp remote:tmp`)

```go
	...
	// issue happens when `transferThrottling` is larger than 1
	transferPool := pool.New().WithContext(ctx).WithMaxGoroutines(transferThrottling)

	remote := rclone.NewRemote(ctx, "webdav", fmt.Sprintf("http://%s:%s", DeviceHost, *backup.TransferPort))

transfer:
	for clientFile, backupFile := range *fileTransferList {
		select {
		case <-ctx.Done():
			break transfer

		default:
			transferPool.Go(
				func(ctx context.Context) error {
					if err := remote.SyncFile(ctx, filepath.Join(folderPath, clientFile), backupFile); err != nil {
						message := fmt.Sprintf("failed to sync file %s to device: %v", clientFile, err)
						logger.Error(message)
						go messageBus.PublishEventWrapper(ctx, EventTypeRunBackupError, map[string]string{PropertyTypeMessage.Name: message})
						return err
					}

					transferRemainingCount--
					eventProperties.Store(PropertyTypeRemainingCount.Name, fmt.Sprintf("%d", transferRemainingCount))

					return nil
				},
			)
		}
	}

	return transferPool.Wait()

```

and here is how I construct `Remote`:

```go
import (
	"context"
	"fmt"
	"path"
	"path/filepath"

	"github.com/rclone/rclone/fs"
	"github.com/rclone/rclone/fs/config"
	"github.com/rclone/rclone/fs/operations"
	"github.com/rclone/rclone/fs/rc"
	"github.com/rclone/rclone/fs/sync"

	_ "github.com/rclone/rclone/backend/local" // add local backend
	_ "github.com/rclone/rclone/backend/webdav" // add webdav backend
)

type Remote struct {
	name string
	config *fs.ConfigOut
}

func NewRemote(ctx context.Context, remoteType, remoteURL string) *Remote {
	kv := rc.Params{
		"url": remoteURL,
	}

	opts := config.UpdateRemoteOpt{
		NonInteractive: true,
		Continue: false,
	}

	configOut, err := config.CreateRemote(ctx, remoteType, remoteType, kv, opts)
	if err != nil {
		panic(err)
	}

	return &Remote{
		name: remoteType,
		config: configOut,
	}
}

func (r *Remote) SyncFile(ctx context.Context, srcFilePath, dstFilePath string) error {
	fsrc, err := fs.NewFs(ctx, filepath.Dir(srcFilePath))
	if err != nil {
		return err
	}

	fdst, err := fs.NewFs(ctx, fmt.Sprintf("%s:%s", r.name, path.Dir(dstFilePath)))
	if err != nil {
		return err
	}

	config := fs.GetConfig(ctx)

	// dry run first
	config.DryRun = true
	if err := operations.CopyFile(ctx, fdst, fsrc, path.Base(dstFilePath), filepath.Base(srcFilePath)); err != nil {
		return err
	}

	// then run for real
	config.DryRun = false
	return operations.CopyFile(ctx, fdst, fsrc, path.Base(dstFilePath), filepath.Base(srcFilePath))
}

```

#### The rclone config contents with secrets removed.

```go
	// configure rclone
	{
		globalConfig := fs.GetConfig(context.TODO())
		globalConfig.LogLevel = fs.LogLevelWarning // rclone logs everything to stderr, thus we need to set this to warning to avoid pollution.

		localConfig := fs.GetConfig(ctx) // if ctx doesn't have any config, this is equivalent to globalConfig
		localConfig.LogLevel = fs.LogLevelWarning // still setting this, just in case someone adds a local config to ctx (who knows?)
	}

```

#### A log from the command with the `-vv` flag

```auto
2023/11/24 10:52:45 ERROR : instrumenter.js: corrupted on transfer: sizes differ 1704 vs 0
2023/11/24 10:52:45 ERROR: failed to sync file resources\app\node_modules\better-ajv-errors\node_modules\core-js\web\dom-collections.js to device: corrupted on transfer: sizes differ 1704 vs 0
2023/11/24 10:52:47 ERROR : index.js: corrupted on transfer: sizes differ 4618 vs 0
2023/11/24 10:52:47 ERROR: failed to sync file resources\app\node_modules\@azure\core-client\dist-esm\src\interfaceHelpers.js to device: corrupted on transfer: sizes differ 4618 vs 0
2023/11/24 10:52:47 ERROR : index.js: Failed to copy: object not found
2023/11/24 10:52:47 ERROR: failed to sync file resources\app\node_modules\better-ajv-errors\node_modules\core-js\internals\get-set-iterator.js to device: object not found

```

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [November 24, 2023, 9:15am UTC](https://forum.rclone.org/t/thread-safety-issue-in-webdav-syncing/43050/2 "2023-11-24T09:15:53Z")

</div>

> [@wxh](#):
>
> Then the problem comes - the code is keep getting errors like
> 
> ```auto
> 2023/11/24 10:52:45 ERROR : instrumenter.js: corrupted on transfer: sizes differ 1704 vs 0
> 2023/11/24 10:52:45 ERROR: failed to sync file resources\app\node_modules\better-ajv-errors\node_modules\core-js\web\dom-collections.js to device: corrupted on transfer: sizes differ 1704 vs 0
> 
> ```

When this happens when using the rclone binary it usually means the source file changed while being transferred. Is that possible?

> [@wxh](#):
>
> It looks like a thread safety issue somewhere in the `rclone` code. If anyone can give some guidance, I am happy to dig deeper.

I would be surprised if there was a thread safety problem as rclone is highly concurrent and is tested with the race detector, however it is easy to check with go - just build with [the race detector](https://go.dev/doc/articles/race_detector) and you'll find out.

Note that rclone wasn't designed to be used as a library like this.

The recommended way of using it is to use [librclone](https://pkg.go.dev/github.com/rclone/rclone/librclone/librclone) which will give you an in memory version of [rclone's API](https://rclone.org/rc/).

I note that you are building new `Fs` objects on each call - this is very inefficient - you should be caching them and re-using them. Rclone will do this for you if you use the API/librclone.

---

<div class="post-metadata">

**Author:** ![wxh](https://forum.rclone.org/user_avatar/forum.rclone.org/wxh/32/14865_2.png) [@wxh](https://forum.rclone.org/u/wxh)\
**Post date:** [November 24, 2023, 9:20am UTC](https://forum.rclone.org/t/thread-safety-issue-in-webdav-syncing/43050/3 "2023-11-24T09:20:49Z")

</div>

Does this `librclone` rely on a separate rclone service? or does it include the rclone logics per se?

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [November 24, 2023, 9:33am UTC](https://forum.rclone.org/t/thread-safety-issue-in-webdav-syncing/43050/4 "2023-11-24T09:33:37Z")

</div>

No, librclone is included into your code. It is an in memory interface to the rclone API - no external service required.

---

<div class="post-metadata">

**Author:** ![wxh](https://forum.rclone.org/user_avatar/forum.rclone.org/wxh/32/14865_2.png) [@wxh](https://forum.rclone.org/u/wxh)\
**Post date:** [November 27, 2023, 2:47am UTC](https://forum.rclone.org/t/thread-safety-issue-in-webdav-syncing/43050/5 "2023-11-27T02:47:59Z")

</div>

Just to be super clear - by "no external service required" you mean I don't even need a rclone service running aside from my code, correct? That is, everything including actual syncing logic, is inclusive in the librclone, correct?

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [November 29, 2023, 9:47am UTC](https://forum.rclone.org/t/thread-safety-issue-in-webdav-syncing/43050/6 "2023-11-29T09:47:53Z")

</div>

> [@wxh](#):
>
> everything including actual syncing logic, is inclusive in the librclone

That is correct.

---

<div class="post-metadata">

**Author:** ![wxh](https://forum.rclone.org/user_avatar/forum.rclone.org/wxh/32/14865_2.png) [@wxh](https://forum.rclone.org/u/wxh)\
**Post date:** [November 29, 2023, 9:50am UTC](https://forum.rclone.org/t/thread-safety-issue-in-webdav-syncing/43050/7 "2023-11-29T09:50:33Z")

</div>

I'm looking at the example of librclone:

```go
func main() {
  librclone.Initialize()
    syncRequest: = syncRequest {
    SrcFs: "<absolute_path>",
    DstFs: ":s3,env_auth=false,access_key_id=<access>,secret_access_key=<secret>,endpoint='<endpoint>':<bucket>",
    }

    syncRequestJSON, err: = json.Marshal(syncRequest)
    if err != nil {
    fmt.Println(err)
    }
		
    out, status: = librclone.RPC("sync/copy", string(syncRequestJSON))
    fmt.Println("Got status : %d and output %q", status, out)
}

```

I tried to look for the struct `syncRequest` but couldn't find it. I searched the repo but didn't find anything either. Can you help?

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [December 6, 2023, 9:08am UTC](https://forum.rclone.org/t/thread-safety-issue-in-webdav-syncing/43050/8 "2023-12-06T09:08:54Z")

</div>

> [@wxh](#):
>
> I tried to look for the struct `syncRequest` but couldn't find it. I searched the repo but didn't find anything either. Can you help?

Rclone doesn't have structs defined for these. It probably should but it doesn't. I suggest you just make a `map[string]string` and use that.

---

<div class="post-metadata">

**Author:** ![wxh](https://forum.rclone.org/user_avatar/forum.rclone.org/wxh/32/14865_2.png) [@wxh](https://forum.rclone.org/u/wxh)\
**Post date:** [December 7, 2023, 2:26am UTC](https://forum.rclone.org/t/thread-safety-issue-in-webdav-syncing/43050/9 "2023-12-07T02:26:18Z")

</div>

OK, will give that a try.

How does my code receive real-time stats like speeds and counts? Is that possible with `librclone`?

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [December 8, 2023, 2:28pm UTC](https://forum.rclone.org/t/thread-safety-issue-in-webdav-syncing/43050/10 "2023-12-08T14:28:52Z")

</div>

Sure, just call [core/stats](https://rclone.org/rc/#core-stats).

You can give your jobs a group id and then get stats just for that group if you want also.

---

<div class="post-metadata">

**Author:** ![system](https://forum.rclone.org/uploads/default/original/2X/3/39ad3235f6ffec76ccd0c81616bd55697b4c300d.png) [@system](https://forum.rclone.org/u/system)\
**Post date:** [January 7, 2024, 2:29pm UTC](https://forum.rclone.org/t/thread-safety-issue-in-webdav-syncing/43050/11 "2024-01-07T14:29:09Z")

</div>

Closed on January 7, 2024, 2:29pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
