Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion server/internal/usecase/interactor/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"net/url"
"strings"
"time"

accountsGateway "github.com/reearth/reearth-accounts/server/pkg/gateway"
accountsID "github.com/reearth/reearth-accounts/server/pkg/id"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/reearth/reearth/server/pkg/project"
"github.com/reearth/reearth/server/pkg/scene"

"github.com/reearth/reearthx/log"
"github.com/reearth/reearthx/rerror"
"github.com/reearth/reearthx/usecasex"
)
Expand Down Expand Up @@ -176,8 +178,23 @@ func (i commonSceneLock) UpdateSceneLock(ctx context.Context, s id.SceneID, befo
return nil
}

// sceneLockReleaseTimeout bounds the lock-release write below, in case the
// detached context's SaveLock call is itself slow or hangs.
const sceneLockReleaseTimeout = 10 * time.Second

// ReleaseSceneLock detaches ctx from its parent's cancellation before saving
// the lock. Callers defer this from publish flows using the same request
// context the publish itself was using (REL-03, compliance scan); if that
// request context is canceled (e.g. the client disconnected mid upload), a
// SaveLock call on the bare ctx would fail with "context canceled" and leave
// the scene locked forever, since sceneLock has no TTL and the only other
// unlock path runs solely at process startup.
func (i commonSceneLock) ReleaseSceneLock(ctx context.Context, s id.SceneID) {
_ = i.sceneLockRepo.SaveLock(ctx, s, scene.LockModeFree)
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), sceneLockReleaseTimeout)
defer cancel()
if err := i.sceneLockRepo.SaveLock(ctx, s, scene.LockModeFree); err != nil {
log.Errorfc(ctx, "failed to release scene lock for %s: %v", s, err)
}
}

type SceneDeleter struct {
Expand Down
50 changes: 50 additions & 0 deletions server/internal/usecase/interactor/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package interactor

import (
"context"
"testing"

"github.com/reearth/reearth/server/pkg/id"
"github.com/reearth/reearth/server/pkg/scene"
"github.com/stretchr/testify/assert"
)

type recordingSceneLockRepo struct {
saveLockCtxErr error
}

func (r *recordingSceneLockRepo) GetLock(context.Context, id.SceneID) (scene.LockMode, error) {
return scene.LockModeFree, nil
}

func (r *recordingSceneLockRepo) GetAllLock(context.Context, id.SceneIDList) ([]scene.LockMode, error) {
return nil, nil
}

func (r *recordingSceneLockRepo) SaveLock(ctx context.Context, _ id.SceneID, _ scene.LockMode) error {
r.saveLockCtxErr = ctx.Err()
return ctx.Err()
}

func (r *recordingSceneLockRepo) ReleaseAllLock(context.Context) error {
return nil
}

// TestReleaseSceneLock_SurvivesCanceledContext is a regression test for REL-03:
// ReleaseSceneLock is deferred from publish flows using the same request context
// the publish itself used. If that request's context is already canceled (e.g.
// the client disconnected mid upload), calling SaveLock on the bare context fails
// with "context canceled" and leaves the scene stuck locked forever. This confirms
// the SaveLock call underneath still runs against a live context even when the
// caller's context is canceled first.
func TestReleaseSceneLock_SurvivesCanceledContext(t *testing.T) {
repo := &recordingSceneLockRepo{}
d := commonSceneLock{sceneLockRepo: repo}

ctx, cancel := context.WithCancel(context.Background())
cancel()

d.ReleaseSceneLock(ctx, id.NewSceneID())

assert.NoError(t, repo.saveLockCtxErr)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saveLockCtxErr starts as nil (Go zero value). If a future change removes or skips the SaveLock call, this assert.NoError still passes because nil == no error. The test does not check that SaveLock was actually called. Adding a called bool field to the mock and asserting assert.True(t, repo.called) would catch that case.