Anders Hattestad
Jan 13, 2014
  3590
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024