Skip to content

Commit 334ce6a

Browse files
author
Daniel Neto
committed
fix: Prevent reentry in getVideoTags and getVideoTagsInProgress tracking
1 parent e2c5799 commit 334ce6a

2 files changed

Lines changed: 106 additions & 73 deletions

File tree

objects/functionTags.php

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,19 @@ function getVideosTagsRating($video)
239239

240240
function getVideoTags($videos_id, $type = '')
241241
{
242-
global $advancedCustom, $advancedCustomUser, $getTags_;
242+
global $advancedCustom, $advancedCustomUser, $getTags_, $_getVideoTagsFunctionInProgress;
243243
$tolerance = 0.1;
244244
$tags = [];
245245

246+
if (empty($_getVideoTagsFunctionInProgress)) {
247+
$_getVideoTagsFunctionInProgress = [];
248+
}
249+
$inProgressKey = "{$videos_id}_{$type}";
250+
if (!empty($_getVideoTagsFunctionInProgress[$inProgressKey])) {
251+
_error_log("getVideoTags($videos_id, $type) reentry detected, returning empty tags", AVideoLog::$WARNING, true);
252+
return [];
253+
}
254+
246255
$cacheSuffix = "getTags_{$type}";
247256
$videoCache = new VideoCacheHandler('', $videos_id);
248257
$oneToFiveHours = rand(3600, 18000); // 1 to 5 hours
@@ -254,60 +263,71 @@ function getVideoTags($videos_id, $type = '')
254263
return $getTags_;
255264
}
256265

257-
$timeName1 = TimeLogStart("getVideoTags {$videos_id}, $type");
258-
if (empty($advancedCustomUser)) {
259-
$advancedCustomUser = AVideoPlugin::getObjectData("CustomizeUser");
260-
}
261-
if (empty($advancedCustom)) {
262-
$advancedCustom = AVideoPlugin::getObjectData("CustomizeAdvanced");
263-
}
264-
$currentPage = getCurrentPage();
265-
$rowCount = getRowCount();
266-
unsetCurrentPage();
267-
$_REQUEST['rowCount'] = 1000;
268-
269-
$video = new Video("", "", $videos_id);
266+
$_getVideoTagsFunctionInProgress[$inProgressKey] = 1;
267+
$currentPage = null;
268+
$rowCount = null;
269+
$requestChanged = false;
270+
try {
271+
$timeName1 = TimeLogStart("getVideoTags {$videos_id}, $type");
272+
if (empty($advancedCustomUser)) {
273+
$advancedCustomUser = AVideoPlugin::getObjectData("CustomizeUser");
274+
}
275+
if (empty($advancedCustom)) {
276+
$advancedCustom = AVideoPlugin::getObjectData("CustomizeAdvanced");
277+
}
278+
$currentPage = getCurrentPage();
279+
$rowCount = getRowCount();
280+
unsetCurrentPage();
281+
$_REQUEST['rowCount'] = 1000;
282+
$requestChanged = true;
270283

271-
$timeName2 = TimeLogStart("getVideoTagsFromVideo {$videos_id}, $type");
272-
$newTags = getVideoTagsFromVideo($video, $type);
273-
$tags = array_merge($tags, $newTags);
274-
TimeLogEnd($timeName2, __LINE__, $tolerance);
284+
$video = new Video("", "", $videos_id);
275285

276-
$timeName2 = TimeLogStart("getVideoTagVideoStatus {$videos_id}, $type");
277-
$newTags = getVideoTagVideoStatus($video, $type);
278-
$tags = array_merge($tags, $newTags);
279-
TimeLogEnd($timeName2, __LINE__, $tolerance);
286+
$timeName2 = TimeLogStart("getVideoTagsFromVideo {$videos_id}, $type");
287+
$newTags = getVideoTagsFromVideo($video, $type);
288+
$tags = array_merge($tags, $newTags);
289+
TimeLogEnd($timeName2, __LINE__, $tolerance);
280290

281-
$timeName2 = TimeLogStart("getVideoTagsGroups {$videos_id}, $type");
282-
$newTags = getVideoTagsGroups($video, $type);
283-
$tags = array_merge($tags, $newTags);
284-
TimeLogEnd($timeName2, __LINE__, $tolerance);
291+
$timeName2 = TimeLogStart("getVideoTagVideoStatus {$videos_id}, $type");
292+
$newTags = getVideoTagVideoStatus($video, $type);
293+
$tags = array_merge($tags, $newTags);
294+
TimeLogEnd($timeName2, __LINE__, $tolerance);
285295

286-
$timeName2 = TimeLogStart("getVideosTagsCategory {$videos_id}, $type");
287-
$newTags = getVideosTagsCategory($video, $type);
288-
$tags = array_merge($tags, $newTags);
289-
TimeLogEnd($timeName2, __LINE__, $tolerance);
296+
$timeName2 = TimeLogStart("getVideoTagsGroups {$videos_id}, $type");
297+
$newTags = getVideoTagsGroups($video, $type);
298+
$tags = array_merge($tags, $newTags);
299+
TimeLogEnd($timeName2, __LINE__, $tolerance);
290300

291-
$timeName2 = TimeLogStart("getVideoTagsSource {$videos_id}, $type");
292-
$newTags = getVideoTagsSource($video, $type);
293-
$tags = array_merge($tags, $newTags);
294-
TimeLogEnd($timeName2, __LINE__, $tolerance);
301+
$timeName2 = TimeLogStart("getVideosTagsCategory {$videos_id}, $type");
302+
$newTags = getVideosTagsCategory($video, $type);
303+
$tags = array_merge($tags, $newTags);
304+
TimeLogEnd($timeName2, __LINE__, $tolerance);
295305

296-
$timeName2 = TimeLogStart("getVideosTagsRating {$videos_id}");
297-
$newTags = getVideosTagsRating($video);
298-
$tags = array_merge($tags, $newTags);
299-
TimeLogEnd($timeName2, __LINE__, $tolerance);
306+
$timeName2 = TimeLogStart("getVideoTagsSource {$videos_id}, $type");
307+
$newTags = getVideoTagsSource($video, $type);
308+
$tags = array_merge($tags, $newTags);
309+
TimeLogEnd($timeName2, __LINE__, $tolerance);
300310

301-
$timeName2 = TimeLogStart("AVideoPlugin::getVideoTags {$videos_id}");
302-
$newTags = AVideoPlugin::getVideoTags($videos_id);
303-
if (is_array($newTags)) {
311+
$timeName2 = TimeLogStart("getVideosTagsRating {$videos_id}");
312+
$newTags = getVideosTagsRating($video);
304313
$tags = array_merge($tags, $newTags);
305-
}
306-
TimeLogEnd($timeName2, __LINE__, $tolerance);
314+
TimeLogEnd($timeName2, __LINE__, $tolerance);
307315

308-
TimeLogEnd($timeName1, __LINE__, $tolerance * 2);
309-
$_REQUEST['current'] = $currentPage;
310-
$_REQUEST['rowCount'] = $rowCount;
316+
$timeName2 = TimeLogStart("AVideoPlugin::getVideoTags {$videos_id}");
317+
$newTags = AVideoPlugin::getVideoTags($videos_id);
318+
if (is_array($newTags)) {
319+
$tags = array_merge($tags, $newTags);
320+
}
321+
TimeLogEnd($timeName2, __LINE__, $tolerance);
322+
323+
TimeLogEnd($timeName1, __LINE__, $tolerance * 2);
324+
} finally {
325+
if ($requestChanged) {
326+
$_REQUEST['current'] = $currentPage;
327+
$_REQUEST['rowCount'] = $rowCount;
328+
}
329+
unset($_getVideoTagsFunctionInProgress[$inProgressKey]);
330+
}
311331

312332
$videoCache->setCache($tags);
313333
//ObjectYPT::setCache($index, $tags);

plugin/AVideoPlugin.php

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,40 +1981,53 @@ public static function getVideoTags($videos_id)
19811981
if (empty($_getVideoTags)) {
19821982
$_getVideoTags = [];
19831983
}
1984+
global $_getVideoTagsInProgress;
1985+
if (empty($_getVideoTagsInProgress)) {
1986+
$_getVideoTagsInProgress = [];
1987+
}
19841988

19851989
if (isset($_getVideoTags[$videos_id])) {
19861990
$array = $_getVideoTags[$videos_id];
19871991
} else {
1988-
1989-
$cacheSuffix = 'getVideoTags';
1990-
$videoCache = new VideoCacheHandler('', $videos_id);
1991-
$array = $videoCache->getCache($cacheSuffix, rand(86400, 864000));
1992-
1993-
//$name = "getVideoTags{$videos_id}";
1994-
//$array = ObjectYPT::getCache($name, 86400);
1995-
//_error_log("getVideoTags $name ".(empty($array)?"new":"old"));
1996-
if (empty($array)) {
1997-
TimeLogStart("AVideoPlugin::getVideoTags($videos_id)");
1998-
$plugins = Plugin::getAllEnabled();
1999-
$array = [];
2000-
foreach ($plugins as $value) {
2001-
$TimeLog = "AVideoPlugin::getVideoTags($videos_id) {$value['dirName']} ";
2002-
TimeLogStart($TimeLog);
2003-
$p = static::loadPlugin($value['dirName']);
2004-
TimeLogEnd($TimeLog, __LINE__, $tolerance);
2005-
if (is_object($p)) {
2006-
$array = array_merge($array, $p->getVideoTags($videos_id));
1992+
if (!empty($_getVideoTagsInProgress[$videos_id])) {
1993+
_error_log("AVideoPlugin::getVideoTags($videos_id) reentry detected, returning empty tags", AVideoLog::$WARNING, true);
1994+
return [];
1995+
}
1996+
$_getVideoTagsInProgress[$videos_id] = 1;
1997+
1998+
try {
1999+
$cacheSuffix = 'getVideoTags';
2000+
$videoCache = new VideoCacheHandler('', $videos_id);
2001+
$array = $videoCache->getCache($cacheSuffix, rand(86400, 864000));
2002+
2003+
//$name = "getVideoTags{$videos_id}";
2004+
//$array = ObjectYPT::getCache($name, 86400);
2005+
//_error_log("getVideoTags $name ".(empty($array)?"new":"old"));
2006+
if (empty($array)) {
2007+
TimeLogStart("AVideoPlugin::getVideoTags($videos_id)");
2008+
$plugins = Plugin::getAllEnabled();
2009+
$array = [];
2010+
foreach ($plugins as $value) {
2011+
$TimeLog = "AVideoPlugin::getVideoTags($videos_id) {$value['dirName']} ";
2012+
TimeLogStart($TimeLog);
2013+
$p = static::loadPlugin($value['dirName']);
2014+
TimeLogEnd($TimeLog, __LINE__, $tolerance);
2015+
if (is_object($p)) {
2016+
$array = array_merge($array, $p->getVideoTags($videos_id));
2017+
TimeLogEnd($TimeLog, __LINE__, $tolerance);
2018+
}
20072019
TimeLogEnd($TimeLog, __LINE__, $tolerance);
20082020
}
2009-
TimeLogEnd($TimeLog, __LINE__, $tolerance);
2010-
}
2011-
TimeLogEnd("AVideoPlugin::getVideoTags($videos_id)", __LINE__, $tolerance * 2);
2021+
TimeLogEnd("AVideoPlugin::getVideoTags($videos_id)", __LINE__, $tolerance * 2);
20122022

2013-
$videoCache->setCache($array);
2014-
//ObjectYPT::setCache($name, $array);
2015-
$_getVideoTags[$videos_id] = $array;
2016-
} else {
2017-
//$array = object_to_array($array);
2023+
$videoCache->setCache($array);
2024+
//ObjectYPT::setCache($name, $array);
2025+
$_getVideoTags[$videos_id] = $array;
2026+
} else {
2027+
//$array = object_to_array($array);
2028+
}
2029+
} finally {
2030+
unset($_getVideoTagsInProgress[$videos_id]);
20182031
}
20192032
}
20202033
return $array;

0 commit comments

Comments
 (0)