Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit 7bb1713

Browse files
Copilottwisti-dev
andcommitted
fix: ensure snapshot() holds read lock during deep copy
Co-authored-by: twisti-dev <[email protected]>
1 parent 8100793 commit 7bb1713

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

surf-cloud-core/surf-cloud-core-common/src/main/kotlin/dev/slne/surf/cloud/core/common/player/CommonCloudPlayerImpl.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ abstract class CommonCloudPlayerImpl(uuid: UUID, override val name: String) :
2222
protected val persistentDataView = object : PersistentPlayerDataContainerViewImpl() {
2323
override fun toTagCompound() = ppdcReentrantLock.read { ppdc.tag }
2424
override fun getTag(key: String) = ppdcReentrantLock.read { ppdc.getTag(key) }
25+
override fun snapshotTag() = ppdcReentrantLock.read { super.snapshotTag() }
2526
}
2627

2728
override val persistentData get() = persistentDataView

surf-cloud-core/surf-cloud-core-common/src/main/kotlin/dev/slne/surf/cloud/core/common/player/ppdc/PersistentPlayerDataContainerViewImpl.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,27 @@ abstract class PersistentPlayerDataContainerViewImpl : PersistentPlayerDataConta
114114
}
115115

116116
override fun snapshot(): PersistentPlayerDataContainerViewImpl {
117-
val tag = deepCopy(toTagCompound())
118-
119-
val tagCopy = CompoundBinaryTag.builder()
120-
.put(tag)
121-
.build()
117+
val tagCopy = snapshotTag()
122118

123119
return object : PersistentPlayerDataContainerViewImpl() {
124120
override fun toTagCompound() = tagCopy
125121
override fun getTag(key: String) = tagCopy.get(key)
122+
override fun snapshotTag() = tagCopy
126123
}
127124
}
128125

126+
/**
127+
* Creates a snapshot of the tag compound.
128+
* Subclasses should override this method to ensure the snapshot is created
129+
* while holding appropriate locks to prevent concurrent modifications.
130+
*/
131+
protected open fun snapshotTag(): CompoundBinaryTag {
132+
val tag = deepCopy(toTagCompound())
133+
return CompoundBinaryTag.builder()
134+
.put(tag)
135+
.build()
136+
}
137+
129138

130139
/**
131140
* Creates a deep copy of the provided `CompoundBinaryTag` without using recursion.

0 commit comments

Comments
 (0)