Attachment adding to 'add-post page'

Looks like something wrong with lib/attachment.php
Function attach_file_to_post() passes $post_id of add-post page instead the new post id returned by wp_insert_post.
As result in media library images are attached to add post page, any ideas how to fix it?