Skip to content

Commit cd7acb1

Browse files
authored
Fix Frame.ChildFrames concurrency issues (#1697)
1 parent fffdcaf commit cd7acb1

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

lib/PuppeteerSharp/Frame.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Threading.Tasks;
45
using Newtonsoft.Json.Linq;
56
using PuppeteerSharp.Input;
@@ -39,6 +40,7 @@ namespace PuppeteerSharp
3940
public class Frame
4041
{
4142
private readonly CDPSession _client;
43+
private readonly List<Frame> _childFrames = new List<Frame>();
4244

4345
internal string Id { get; set; }
4446

@@ -66,15 +68,24 @@ internal Frame(FrameManager frameManager, CDPSession client, Frame parentFrame,
6668

6769
if (parentFrame != null)
6870
{
69-
ParentFrame.ChildFrames.Add(this);
71+
ParentFrame.AddChildFrame(this);
7072
}
7173
}
7274

7375
#region Properties
7476
/// <summary>
7577
/// Gets the child frames of the this frame
7678
/// </summary>
77-
public List<Frame> ChildFrames { get; } = new List<Frame>();
79+
public List<Frame> ChildFrames
80+
{
81+
get
82+
{
83+
lock (_childFrames)
84+
{
85+
return _childFrames.ToList();
86+
}
87+
}
88+
}
7889

7990
/// <summary>
8091
/// Gets the frame's name attribute as specified in the tag
@@ -495,6 +506,22 @@ public Task ClickAsync(string selector, ClickOptions options = null)
495506
public Task TypeAsync(string selector, string text, TypeOptions options = null)
496507
=> SecondaryWorld.TypeAsync(selector, text, options);
497508

509+
internal void AddChildFrame(Frame frame)
510+
{
511+
lock (_childFrames)
512+
{
513+
_childFrames.Add(frame);
514+
}
515+
}
516+
517+
internal void RemoveChildFrame(Frame frame)
518+
{
519+
lock (_childFrames)
520+
{
521+
_childFrames.Remove(frame);
522+
}
523+
}
524+
498525
internal void OnLoadingStopped()
499526
{
500527
LifecycleEvents.Add("DOMContentLoaded");
@@ -527,7 +554,7 @@ internal void Detach()
527554
SecondaryWorld.Detach();
528555
if (ParentFrame != null)
529556
{
530-
ParentFrame.ChildFrames.Remove(this);
557+
ParentFrame.RemoveChildFrame(this);
531558
}
532559
ParentFrame = null;
533560
}

0 commit comments

Comments
 (0)