[skip ci] Fix memory leak in MarkVideoWatchedTask

This commit is contained in:
Brahim Hadriche committed 2025-06-21 21:20:33 -04:00
1 parent 9f5efb0ba7
commit 8f8a8980df
6 files changed
+57 -30

No files matched your search

+1
View File
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Invidious tests - `Me at the zoo` video was getting blocked - Invidious tests - `Me at the zoo` video was getting blocked
- Bug where logging out from inactive profile is not properly persisted - Bug where logging out from inactive profile is not properly persisted
- Memory leak in MarkVideoWatchedTask
### Removed ### Removed
@@ -3,7 +3,7 @@ import "pkg:/source/utils/MessagePortUtils.bs"
import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/StringUtils.bs"
' Maximum number of JobTasks that can be created ' Maximum number of JobTasks that can be created
const MAX_JOB_TASK_COUNT = 15 const MAX_JOB_TASK_COUNT = 20
' Minimum number of JobTasks that will be kept alive ' Minimum number of JobTasks that will be kept alive
const MIN_JOB_TASK_COUNT = 2 const MIN_JOB_TASK_COUNT = 2
' Time in milliseconds to wait before scaling down JobTasks ' Time in milliseconds to wait before scaling down JobTasks
@@ -2,23 +2,26 @@ import "pkg:/components/Services/Innertube/InnertubeService.bs"
import "pkg:/components/Services/Invidious/InvidiousService.bs" import "pkg:/components/Services/Invidious/InvidiousService.bs"
import "pkg:/source/utils/MessagePortUtils.bs" import "pkg:/source/utils/MessagePortUtils.bs"
@asynctask @job("MarkVideoWatchedJob")
function MarkVideoWatchedTask(input as object) as object function ExecuteJob() as void
input = JobGetInput()
videoId = input.videoId videoId = input.videoId
invidiousNode = input.invidious invidiousNode = input.invidious
authToken = input.authToken authToken = input.authToken
profileType = input.profileType profileType = input.profileType
cancellation = m.top.cancellation
cancellation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true)
if profileType = "invidious" if profileType = "invidious"
service = new Invidious.InvidiousService(invidiousNode) service = new Invidious.InvidiousService(invidiousNode)
service.MarkVideoWatched(videoId, cancellation) service.MarkVideoWatched(videoId, cancellation)
return invalid return
end if end if
if profileType <> "youtube" if profileType <> "youtube"
LogError("MarkVideoWatchedTask: Unsupported profile type:", profileType) JobError(`Unsupported profile type: ${profileType}`)
return invalid return
end if end if
playbackTrackingUrls = InnertubeService.AuthGetPlaybackTrackingUrls(videoId, { playbackTrackingUrls = InnertubeService.AuthGetPlaybackTrackingUrls(videoId, {
@@ -28,13 +31,13 @@ function MarkVideoWatchedTask(input as object) as object
}) })
if playbackTrackingUrls.error <> invalid if playbackTrackingUrls.error <> invalid
LogError("MarkVideoWatchedTask: Failed to get playback tracking URLs:", playbackTrackingUrls.error) JobError(`Failed to get playback tracking URLs: ${playbackTrackingUrls.error}`)
return invalid return
end if end if
if StringUtils.IsNullOrEmpty(playbackTrackingUrls.videostatsPlaybackUrl) if StringUtils.IsNullOrEmpty(playbackTrackingUrls.videostatsPlaybackUrl)
LogError("MarkVideoWatchedTask: videostatsPlaybackUrl is empty") JobError("videostatsPlaybackUrl is empty")
return invalid return
end if end if
markedAsWatched = InnertubeService.AuthMarkVideoWatched(playbackTrackingUrls.videostatsPlaybackUrl, { markedAsWatched = InnertubeService.AuthMarkVideoWatched(playbackTrackingUrls.videostatsPlaybackUrl, {
@@ -43,25 +46,29 @@ function MarkVideoWatchedTask(input as object) as object
cancellation: cancellation cancellation: cancellation
}) })
if not markedAsWatched if not markedAsWatched
return invalid JobError("Failed to mark video as watched")
return
end if end if
if StringUtils.IsNullOrEmpty(playbackTrackingUrls.videostatsWatchtimeUrl) if StringUtils.IsNullOrEmpty(playbackTrackingUrls.videostatsWatchtimeUrl)
LogError("MarkVideoWatchedTask: videostatsWatchtimeUrl is empty") JobError("videostatsWatchtimeUrl is empty")
return invalid return
end if end if
if CancellationUtils.IsCancelled(cancellation) if CancellationUtils.IsCancelled(cancellation)
return invalid JobCancel()
return
end if end if
port = CreateObject("roMessagePort") port = CreateObject("roMessagePort")
MessagePortUtils.ObserveFieldScopedExSafe(m.top, "watchTime", port) callbackNode = JobGetCallbackNode()
MessagePortUtils.ObserveFieldScopedExSafe(callbackNode, "watchTime", port)
CancellationUtils.ObserveCancellationScoped(cancellation, port) CancellationUtils.ObserveCancellationScoped(cancellation, port)
while true while true
message = wait(60000, port) message = wait(60000, port)
if CancellationUtils.IsCancellationEvent(cancellation, message) if CancellationUtils.IsCancellationEvent(cancellation, message)
return invalid JobCancel()
return
end if end if
if message = invalid if message = invalid
continue while continue while
@@ -70,8 +77,10 @@ function MarkVideoWatchedTask(input as object) as object
InnertubeService.AuthMarkVideoWatchTime(watchTime, playbackTrackingUrls.videostatsWatchtimeUrl, { InnertubeService.AuthMarkVideoWatchTime(watchTime, playbackTrackingUrls.videostatsWatchtimeUrl, {
accessToken: authToken.token accessToken: authToken.token
onBehalfOfUser: authToken.activeIdentifyToken onBehalfOfUser: authToken.activeIdentifyToken
cancellation: cancellation ' TODO:P1 if we pass cancellation here, http client will remove scoped
' observer, which is stored on the current node.
' This requires an extra observer node to be independent.
cancellation: invalid
}) })
end while end while
return invalid
end function end function
@@ -330,19 +330,22 @@ function MarkVideoWatched(videoId as dynamic) as void
return return
end if end if
if m.markVideoWatchedTask <> invalid if m.markVideoWatchedJob <> invalid
m.markVideoWatchedTask.cancel = true m.markVideoWatchedJob.cancel = true
end if end if
profileType = authToken.profileNode.type profileType = authToken.profileNode.type
m.markVideoWatchedTask = AsyncTask.Create(Tasks.MarkVideoWatchedTask, {
input = {
videoId: videoId videoId: videoId
invidious: invidious invidious: invidious
authToken: authToken authToken: authToken
profileType: profileType profileType: profileType
}) }
m.markVideoWatchedTask.addField("watchTime", "float", false)
m.markVideoWatchedTask.control = "RUN" m.markVideoWatchedJob = JobSystem.CreateCallback()
m.markVideoWatchedJob.addField("watchTime", "float", false)
JobSystem.QueueJob(m.jobQueue, Jobs.MarkVideoWatchedJob, input, m.markVideoWatchedJob)
if profileType <> "youtube" if profileType <> "youtube"
return return
@@ -355,7 +358,7 @@ function MarkVideoWatched(videoId as dynamic) as void
end function end function
function OnPositionChangeMarkWatchTime(event as object) as void function OnPositionChangeMarkWatchTime(event as object) as void
if m.markVideoWatchedTask = invalid if m.markVideoWatchedJob = invalid
return return
end if end if
@@ -365,7 +368,7 @@ function OnPositionChangeMarkWatchTime(event as object) as void
if currentWatchTimeMarker = currentPosition if currentWatchTimeMarker = currentPosition
return return
end if end if
m.markVideoWatchedTask.watchTime = m.watchTimeMarker m.markVideoWatchedJob.watchTime = m.watchTimeMarker
end function end function
function OnClose() function OnClose()
@@ -373,9 +376,9 @@ function OnClose()
m.top.previousPlayer = invalid m.top.previousPlayer = invalid
if m.markVideoWatchedTask <> invalid if m.markVideoWatchedJob <> invalid
m.markVideoWatchedTask.cancel = true m.markVideoWatchedJob.cancel = true
m.markVideoWatchedTask = invalid m.markVideoWatchedJob = invalid
end if end if
if m.videoContentTask <> invalid if m.videoContentTask <> invalid
m.videoContentTask.cancel = true m.videoContentTask.cancel = true
@@ -27,4 +27,12 @@ namespace Assert
end if end if
end function end function
function NodeHasField(node as dynamic, field as string, message = invalid as dynamic) as void
if node = invalid
throw "Assertion failed: Node is invalid. " + ToString(message)
end if
if not node.hasField(field)
throw "Assertion failed: Node does not have field '" + field + "'. " + ToString(message)
end if
end function
end namespace end namespace
@@ -1,6 +1,12 @@
import "AssertUtils.bs"
namespace MessagePortUtils namespace MessagePortUtils
function ObserveFieldScopedExSafe(node as dynamic, field as string, port as dynamic) as void function ObserveFieldScopedExSafe(node as dynamic, field as string, port as dynamic) as void
#if DEBUG
Assert.IsValid(node, "Node is invalid")
Assert.NodeHasField(node, field, `Node does not have field "${field}"`)
#end if
' ObserveFieldScopedEx was added in 12.0 ' ObserveFieldScopedEx was added in 12.0
if FindMemberFunction(node, "ObserveFieldScopedEx") <> invalid if FindMemberFunction(node, "ObserveFieldScopedEx") <> invalid
' bs:disable-next-line NO_OBSERVE_FIELD_SCOPED_EX ' bs:disable-next-line NO_OBSERVE_FIELD_SCOPED_EX