I've found an alternative solution:The focus bug happens if I try to focus() right after creating the dialog.
So I create the dialog in advance, with autoOpen:false as a parameter
$dialog.dialog({autoOpen:false});
and when I need to show it I use:
$dialog.dialog('open');
$input.focus();