Anders Hattestad
Jan 13, 2014
  2907
(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
Optimizely SendGrid SMTP host is deprecated

SendGrid is a services for sending email that is included in Optimizely DXP. Previously smtp.episerver.net was the recommended SMTP server to use,...

Tomas Hensrud Gulla | Dec 4, 2022 | Syndicated blog

Hosting Optimizely CMS 12 on Docker Engine

Since Optimizely CMS can now be deployed as a Docker container, here is a demonstration of building, running and scaling an Optimizely CMS 12 site ...

Stefan Holm Olsen | Dec 4, 2022 | Syndicated blog

How to use CacheTagHelper with content areas in Optimizely CMS 12

I might be going out on a limb here - if you have a better solution, feel very free to share it!  Upgrading your Optimizely web application from .N...

Andreas J | Dec 2, 2022

The 1001st Piece in your 1000 Piece Puzzle: .NET Default Interface Functions

I was recently working with a client who wanted a reasonably large subsystem added to Optimizely that would add automated management to their...

Greg J | Nov 28, 2022 | Syndicated blog