Anders Hattestad
Jan 13, 2014
  2819
(0 votes)

Error: Deadlock risk detected…..

I got this error “Deadlock risk detected.Trying to read a page from the same thread that is already trying to read the same page”  and want to share with you what was my problem.

I can seems that when you first access a page data, and if that page data object make a call to itself (direct or indirect) you will get this error.

I got it in my file selector property and I needed to rewrite the Value method.

The problem was that if you in the file selector added files from the page folder to the page the

System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetDirectory(“/pagefiles/currentPageID/”) this code will make a new call to get the page and make a deadlock.

The trick is to move the code from Value to the SaveData method. So instead of changing the code each time it’s read from the database, only to each time it’s saved.

So the new code for instead of the Value method in  the PropertyFiles.cs class is

Code Snippet
  1. public override object SaveData(PropertyDataCollection properties)
  2. {
  3.     var str = this.LongString;
  4.  
  5.     if (str != null)
  6.     {
  7.         var doc = new HtmlDocument();
  8.         doc.LoadHtml(str);
  9.         var refLinks = "";
  10.         foreach (var item in doc.DocumentNode.ChildNodes)
  11.         {
  12.             if (item.Name == "div" && item.Attributes.Contains("data-href") && item.Attributes["data-href"].Value != "")
  13.             {
  14.                 var href = item.Attributes["data-href"].Value;
  15.                 var inner = "";
  16.                 refLinks += "<a href=\"" + href + "\" class=\"ignore\" style=\"display:none;\">" + href + "</a>";
  17.                 item.Attributes.Remove("style");
  18.  
  19.  
  20.                 if (href.EndsWith("/"))
  21.                 {
  22.                     item.Attributes.Add("style", "border:1px solid black;margin-bottom:5px;");
  23.  
  24.                     inner = "<div>";
  25.                     inner += "<input type=checkbox onclick=\"$(this).parent().parent().removeAttr('data-href');$(this).parent().parent().css('display','none');var obj=$(this).closest('.ownerDiv').find('.actionField');obj.val('" + href + "');obj.click();\" />delete";
  26.  
  27.                     inner += "</div>";
  28.                     var dir = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetDirectory(href) as UnifiedDirectory;
  29.                     if (dir != null)
  30.                     {
  31.                         inner += "All files from folder:<br />" + dir.VirtualPath;
  32.                         foreach (UnifiedFile fil2 in dir.Files)
  33.                         {
  34.  
  35.                             if (BaseMedia.IsMedia(fil2.VirtualPath))
  36.                             {
  37.                                 refLinks += "<a href=\"" + fil2.VirtualPath + "\" class=\"ignore\" style=\"display:none;\">" + fil2.VirtualPath + "</a>";
  38.                                 inner += "<div style=\"float:left;\">";
  39.                                 inner += "<img style=\"width:80px;height:80px;\"  src=\"/FileCache" + fil2.VirtualPath + "/width_80.height_80.mode_crop.jpg\" data-org=\"" + fil2.VirtualPath + "\" title=\"" + fil2.VirtualPath + "\" />";
  40.                                 inner += "</div>";
  41.                             }
  42.                             else
  43.                             {
  44.                                 refLinks += "<a href=\"" + fil2.VirtualPath + "\" class=\"ignore\" style=\"display:none;\">" + fil2.VirtualPath + "</a>";
  45.                                 inner += "<div title=\"" + fil2.VirtualPath + "\">" + fil2.Name + "</div>";
  46.                             }
  47.                         }
  48.                     }
  49.                 }
  50.                 else
  51.                 {
  52.                     item.Attributes.Add("style", "border:1px solid black;float:left;margin-right:5px;margin-bottom:5px;");
  53.                     var filname = Path.GetFileName(href);
  54.                     var path = href.Substring(0, href.Length - filname.Length);
  55.                     inner = "<div>";
  56.                     inner += "<input type=checkbox onclick=\"$(this).parent().parent().removeAttr('data-href');$(this).parent().parent().css('display','none');var obj=$(this).closest('.ownerDiv').find('.actionField');obj.val('" + href + "');obj.click();\" />delete";
  57.  
  58.                     inner += "<input type=checkbox onclick=\"$(this).parent().parent().attr('data-href','" + path + "');var obj=$(this).closest('.ownerDiv').find('.actionField');$(this).parent().html('All files from folder:<br />" + path + "');obj.val('" + href + "');obj.click();\" />all<br/>";
  59.  
  60.  
  61.  
  62.                     item.Attributes.Remove("onclick");
  63.                     if (BaseMedia.IsMedia(filname))
  64.                     {
  65.                         inner += "<img style=\"width:80px;height:80px;\" src=\"/FileCache" + href + "/width_80.height_80.mode_crop.jpg\" data-org=\"" + href + "\" title=\"" + href + "\" />";
  66.  
  67.                     }
  68.                     else
  69.                     {
  70.                         inner += "<div title=\"" + href + "\">" + filname + "</div>";
  71.                     }
  72.                     inner += "</div>";
  73.                 }
  74.  
  75.                 item.InnerHtml = inner + "<div style=\"clear:both;\"></div>";
  76.  
  77.             }
  78.         }
  79.         return doc.DocumentNode.OuterHtml;
  80.              
  81.     }
  82.     else
  83.     {
  84.         return this.Value;
  85.     }
  86. }
Jan 13, 2014

Comments

Apr 24, 2017 04:47 PM

Please login to comment.
Latest blogs
Content Delivery API – The Case of the Duplicate API Refresh Token

Creating a custom refresh provider to resolve the issues with duplicate tokens in the DXC The post Content Delivery API – The Case of the Duplicate...

David Lewis | Sep 29, 2022 | Syndicated blog

New Optimizely certifications - register for beta testing before November 1st

In January 2023, Optimizely is making updates to the current versions of our certification exams to make sure that each exam covers the necessary...

Jamilia Buzurukova | Sep 28, 2022

Optimizely community meetup - Sept 29 (virtual + Melbourne)

Super excited to be presenting this Thursday the 29th of September at the Optimizely community meetup. For the full details and RSVP's see the...

Ynze | Sep 27, 2022 | Syndicated blog

Preview multiple Visitor Groups directly while browsing your Optimizely site

Visitor groups are great - it's an easy way to add personalization towards market segments to your site. But it does come with it's own set of...

Allan Thraen | Sep 26, 2022 | Syndicated blog